DOM解析xml时在内存中生成DOM文档树,可以随机访问/增加/修改/删除其中的节点,十分方便,缺点是xml十分庞大时,解析会消耗较多内存
Node对象代表着DOM的基本类型,代表文档树中的一个节点(包括空格回车等),Element(元素),Attr(属性),Text(文本),Document(文档)都继承于Node
1.解析xml文档:
books.xml
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book no="01">
<name>JAVA编程思想</name>
<price>100</price>
</book>
<book no="02">
<name>EFFECTIVE JAVA</name>
<price>100</price>
</book>
</books>
先使用解析器生成Document对象:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
//得到document对象
Document document = builder.parse(new File("D:/books.xml"));
解析过程:
static void readXml (Document document){
//得到文档的根元素
Element rootNode = document.getDocumentElement();
//或者
//Node rootNode = document.getElementsByTagName("books").item(0);
//遍历books元素内的子元素
NodeList bookList = rootNode.getChildNodes();
//或者
//NodeList bookList = document.getElementsByTagName("book");
for (int i = 0; i < bookList.getLength(); i++) {
Node bookNode = bookList.item(i);
//去空格
if (bookNode instanceof Element) {
print("--------");
print("元素:");
print(bookNode.getNodeName());
print("属性:");
//获得属性
print(((Element) bookNode).getAttribute("no"));
//获得book节点下的所有元素
for (int j = 0; j < bookNode.getChildNodes().getLength(); j++) {
Node node = bookNode.getChildNodes().item(j);
if (node instanceof Element) {
print(bookNode.getNodeName()+"的子元素:");
print(node.getNodeName());
print(bookNode.getNodeName() +"的子元素值:");
print(node.getTextContent());
}
}
//作用和上面循环相同
// Node child = bookNode.getFirstChild();
// while (child != null) {
// if (child instanceof Element) {
// print(bookNode.getNodeName()+"的子元素:");
// print(child.getNodeName());
// print(bookNode.getNodeName() +"的子元素值:");
// print(child.getTextContent());
// }
// child = child.getNextSibling();
// }
}
}
}
public static void print(Object obj) {
System.out.println(obj.toString());
}
结果:
--------
元素:
book
属性:
01
book的子元素:
name
book的子元素值:
JAVA编程思想
book的子元素:
price
book的子元素值:
100
--------
元素:
book
属性:
02
book的子元素:
name
book的子元素值:
EFFECTIVE JAVA
book的子元素:
price
book的子元素值:
100
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
//------------添加节点------------
Element books = document.createElement("books");
document.appendChild(books);
//属性
Attr attr = document.createAttribute("no");
attr.setValue("03");
books.setAttributeNode(attr);
Element book = document.createElement("book");
books.appendChild(book);
Element name = document.createElement("name");
Element price = document.createElement("price");
//文本值
Text nameText = document.createTextNode("Servlet");
Text priceText = document.createTextNode("200");
name.appendChild(nameText);
price.appendChild(priceText);
book.appendChild(name);
book.appendChild(price);
//利用生成的dom创建dom输入源
DOMSource source = new DOMSource(document);
//保存转换后结果到books.xml文件中
StreamResult result = new StreamResult(new File("books.xml"));
//转换工厂类
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
tf.transform(source, result);
结果:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><books no="03"><book><name>Servlet</name><price>200</price></book></books>