org.w3c.dom解析xml

阅读更多

一直用的dom4j解析,最近碰到工程里面是这种解析方式。

先记录下网上百度的一段代码。

 

写道
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import javax.xml.parsers.*;
import org.w3c.dom.*;
public class edit
{
public static void main(String[] args)
{
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse("links.xml");
doc.normalize();

NodeList books =doc.getDocumentElement().getChildNodes();
Node book;
NodeList lists;

book=books.item(1);

//添加节点
Element id=doc.createElement("id");
id.appendChild(doc.createTextNode("first"));
book.appendChild(id);
//删除节点
book.removeChild(book.getChildNodes().item(1));

//修改节点
Text sina=doc.createTextNode("sina");
book.getChildNodes().item(2).replaceChild(sina,book.getChildNodes().item(2).getFirstChild());

System.out.println(book.getChildNodes().item(2).getNodeName());
System.out.println(books.getLength());
System.out.println(book.getChildNodes().getLength());
//浏览查看
for(int i=1;i{
book=books.item(i);
lists=book.getChildNodes();
for(int j=1;j{
if(lists.item(j).getNodeType()==Node.ELEMENT_NODE)
System.out.println(lists.item(j).getFirstChild().getNodeValue());
}
}

TransformerFactory tFactory =TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new java.io.File("links.xml"));
transformer.transform(source, result);
}catch(Exception e){System.out.println(e);}
}
}

 -----------------------------------------------------------------------------------------------------

有如下的xml文档

        Helvetica

        36

遍历整个xml的所有node

 

 

 

XML文档时,解析器会得到5个结果:

写道
import java.io.File;

import java.io.IOException;


import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;


import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.NamedNodeMap;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;


public class XMLReader {


public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException

{

File file = new File("test.xml");

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();

//将xml文件解释成文档对象

Document doc = db.parse(file);

//取得文档根

doc.normalize();

Element root = doc.getDocumentElement();

//取得要元素名

System.out.println("The root element is:" + root.getNodeName());

//获取孩子节点

NodeList children = root.getChildNodes();

stepThrough(root);

}



private static void stepThrough (Node start)

{

for(Node child = start.getFirstChild();child != null;child = child.getNextSibling())

{

if(child instanceof Element)//去除多余的空白

{

System.out.print("节点名:"+child.getNodeName());

System.out.println("\t节点值:"+ child.getNodeValue());

}

if(child != null)

stepThrough(child);

}

}

}

 在处理这个

 

之间的空白区域

Name元素

之间的空白区域

Size元素

之间的空白区域

如果只希望得到子元素,那么你可以忽略空白字符:

 

 

xml文档节点

写道
for(int i = 0;i < children.getLength(); i++)

{

Node child = children.item(i);

if (child instanceof Element)

{

Element childElement = (Element)child;

......

}

}

 遍历整个

 

for(Node childNode = element.getFirstChild();childNode != null;childNode = childNode.getNextSibling())

{

        ......

}

遍历整个xml某节点的所有属性

 

 

 

写道
NameNodeMap attributes = element.getAttributes();

for (int i = 0;i
{

Node attribute = attributes.item(i);

String name = attribute.getNodeName();//获得属性名

String value = attribute.getNodeValue();//获得属性值

}

 如果只想知道己知属性的值,只需用

 

String unit = element.getAttribute(“unit”);

你可能感兴趣的:(XML,JAVA)