xml文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book id="1" lazy="false">
<name>冰与火之歌</name>
<author>乔治马丁</author>
<year>2014</year>
<price>89</price>
</book>
<book id="2" lazy="true">
<name>安徒生童话</name>
<year>2004</year>
<price>77</price>
<language>English</language>
</book>
</bookstore>
1.使用dom4j解析xml:
/**
* @author zy
*dom4j解析xml文档是性能最好的解析方式
*/
package com.channelsoft.dom4j;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class DOM4jTest {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
//创建SAXReader对象
SAXReader reader = new SAXReader();
Document document= reader.read("dom.xml");
Element root = document.getRootElement();
//获取根节点bookstore
//System.out.println(root.getName());
//通过迭代器遍历
/*Iterator it = root.elementIterator();
while(it.hasNext()) {
Element ele = (Element) it.next();//book节点
List<Attribute> attr = ele.attributes();//book节点所有属性
for (Attribute attribute : attr) {
System.out.println(attribute.getName() + ":" + attribute.getValue());
}
Iterator eleIt = ele.elementIterator();
while(eleIt.hasNext()) {
Element bookChild = (Element) eleIt.next();
System.out.println(bookChild.getName() + ":" + bookChild.getStringValue());
}
}*/
List<Element> elements = root.elements();
for (Element element : elements) {
//System.out.println(element.getName() + ":" + element.getStringValue());
//element包含整个book元素及其子元素
List<Attribute> attrs = element.attributes();//book元素的所有属性
for (Attribute attribute : attrs) {
System.out.println(attribute.getName() + ":" + attribute.getValue());
}
List<Element> bookChilds = element.elements();
for (Element bookChild : bookChilds) {
System.out.println(bookChild.getName() + ":" + bookChild.getStringValue());
}
System.out.println("***********************************");
}
}
}
2.使用dom解析将xml:
/**
* @author zy
*dom解析将xml文档转换成dom树放在内存,因此对于文档太大的xml会耗会太多内存
*/
package com.channelsoft.dom;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class DOMTest {
public static void main(String[] args) throws Exception {
// 创建一个DocumentBuilderFactory对象
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// 创建DocumentBuilder对象
DocumentBuilder db = factory.newDocumentBuilder();
// 将xml加载到当前项目下
Document document = db.parse("dom.xml");
// 遍历多有book节点
NodeList bookList = document.getElementsByTagName("book");
int len = bookList.getLength();
System.out.println("一个有" + len + "个book节点");
for (int i = 0; i < len; i++) {
//获取book节点
Node node = bookList.item(i);
//获取book节点的所有属性
NamedNodeMap nodeMap = node.getAttributes();
for (int j = 0; j < nodeMap.getLength(); j++) {
//遍历book节点的属性
Node child = nodeMap.item(j);
System.out.println(child.getNodeName() + ":" + child.getNodeValue());
}
//获取book节点的所有子节点
NodeList childNodes = node.getChildNodes();
int childLen = childNodes.getLength();
System.out.println("子节点长度:" + childLen);
for(int k = 0; k < childLen; k++) {
Node childNode = childNodes.item(k);//包含五个空白节点(text类型)和四个元素节点
if(childNode.getNodeType() == Node.ELEMENT_NODE) {
System.out.println(childNode.getNodeName() + ":" + childNode.getFirstChild().getNodeValue());
}
}
}
}
}