一、【基础知识——扫盲】
二、【DOM、SAX、JDOM、DOM4j简单使用介绍】
三、【性能测试】
四、【对比】
五、【小插曲XPath】
六、【补充】
目前在Java中用于解析XML的技术很多,主流的有DOM、SAX、JDOM、DOM4j,下文主要介绍这4种解析XML文档技术的使用、优缺点及性能测试。
后文代码中有使用到text.xml(该文档放在src路径下,既编译后在classes路径下),都是指该xml文档。
package test.xml;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;
/**
* dom读写xml
* @author whwang
*/
public class TestDom {
public static void main(String[] args) {
read();
//write();
}
public static void read() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = dbf.newDocumentBuilder();
InputStream in = TestDom.class.getClassLoader().getResourceAsStream("test.xml");
Document doc = builder.parse(in);
// root
Element root = doc.getDocumentElement();
if (root == null) return;
System.err.println(root.getAttribute("name"));
// all college node
NodeList collegeNodes = root.getChildNodes();
if (collegeNodes == null) return;
for(int i = 0; i < collegeNodes.getLength(); i++) {
Node college = collegeNodes.item(i);
if (college != null && college.getNodeType() == Node.ELEMENT_NODE) {
System.err.println("\t" + college.getAttributes().getNamedItem("name").getNodeValue());
// all class node
NodeList classNodes = college.getChildNodes();
if (classNodes == null) continue;
for (int j = 0; j < classNodes.getLength(); j++) {
Node clazz = classNodes.item(j);
if (clazz != null && clazz.getNodeType() == Node.ELEMENT_NODE) {
System.err.println("\t\t" + clazz.getAttributes().getNamedItem("name").getNodeValue());
// all student node
NodeList studentNodes = clazz.getChildNodes();
if (studentNodes == null) continue;
for (int k = 0; k < studentNodes.getLength(); k++) {
Node student = studentNodes.item(k);
if (student != null && student.getNodeType() == Node.ELEMENT_NODE) {
System.err.print("\t\t\t" + student.getAttributes().getNamedItem("name").getNodeValue());
System.err.print(" " + student.getAttributes().getNamedItem("sex").getNodeValue());
System.err.println(" " + student.getAttributes().getNamedItem("age").getNodeValue());
}
}
}
}
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void write() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = dbf.newDocumentBuilder();
InputStream in = TestDom.class.getClassLoader().getResourceAsStream("test.xml");
Document doc = builder.parse(in);
// root
Element root = doc.getDocumentElement();
if (root == null) return;
// 修改属性
root.setAttribute("name", "tsu");
NodeList collegeNodes = root.getChildNodes();
if (collegeNodes != null) {
for (int i = 0; i
该代码只要稍做修改,即可变得更加简洁,无需一直写if来判断是否有子节点。
package test.xml;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
/**
*
* @author whwang
*/
public class TestSAX {
public static void main(String[] args) {
read();
write();
}
public static void read() {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
InputStream in = TestSAX.class.getClassLoader().getResourceAsStream("test.xml");
parser.parse(in, new MyHandler());
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void write() {
System.err.println("纯SAX对于写操作无能为力");
}
}
// 重写对自己感兴趣的事件处理方法
class MyHandler extends DefaultHandler {
@Override
public InputSource resolveEntity(String publicId, String systemId)
throws IOException, SAXException {
return super.resolveEntity(publicId, systemId);
}
@Override
public void notationDecl(String name, String publicId, String systemId)
throws SAXException {
super.notationDecl(name, publicId, systemId);
}
@Override
public void unparsedEntityDecl(String name, String publicId,
String systemId, String notationName) throws SAXException {
super.unparsedEntityDecl(name, publicId, systemId, notationName);
}
@Override
public void setDocumentLocator(Locator locator) {
super.setDocumentLocator(locator);
}
@Override
public void startDocument() throws SAXException {
System.err.println("开始解析文档");
}
@Override
public void endDocument() throws SAXException {
System.err.println("解析结束");
}
@Override
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
super.startPrefixMapping(prefix, uri);
}
@Override
public void endPrefixMapping(String prefix) throws SAXException {
super.endPrefixMapping(prefix);
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
System.err.print("Element: " + qName + ", attr: ");
print(attributes);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
super.endElement(uri, localName, qName);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
super.characters(ch, start, length);
}
@Override
public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException {
super.ignorableWhitespace(ch, start, length);
}
@Override
public void processingInstruction(String target, String data)
throws SAXException {
super.processingInstruction(target, data);
}
@Override
public void skippedEntity(String name) throws SAXException {
super.skippedEntity(name);
}
@Override
public void warning(SAXParseException e) throws SAXException {
super.warning(e);
}
@Override
public void error(SAXParseException e) throws SAXException {
super.error(e);
}
@Override
public void fatalError(SAXParseException e) throws SAXException {
super.fatalError(e);
}
private void print(Attributes attrs) {
if (attrs == null) return;
System.err.print("[");
for (int i = 0; i < attrs.getLength(); i++) {
System.err.print(attrs.getQName(i) + " = " + attrs.getValue(i));
if (i != attrs.getLength() - 1) {
System.err.print(", ");
}
}
System.err.println("]");
}
}
3、【JDOM】
package test.xml;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
/**
* JDom读写xml
* @author whwang
*/
public class TestJDom {
public static void main(String[] args) {
//read();
write();
}
public static void read() {
try {
boolean validate = false;
SAXBuilder builder = new SAXBuilder(validate);
InputStream in = TestJDom.class.getClassLoader().getResourceAsStream("test.xml");
Document doc = builder.build(in);
// 获取根节点
Element root = doc.getRootElement();
readNode(root, "");
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static void readNode(Element root, String prefix) {
if (root == null) return;
// 获取属性
List attrs = root.getAttributes();
if (attrs != null && attrs.size() > 0) {
System.err.print(prefix);
for (Attribute attr : attrs) {
System.err.print(attr.getValue() + " ");
}
System.err.println();
}
// 获取他的子节点
List childNodes = root.getChildren();
prefix += "\t";
for (Element e : childNodes) {
readNode(e, prefix);
}
}
public static void write() {
boolean validate = false;
try {
SAXBuilder builder = new SAXBuilder(validate);
InputStream in = TestJDom.class.getClassLoader().getResourceAsStream("test.xml");
Document doc = builder.build(in);
// 获取根节点
Element root = doc.getRootElement();
// 修改属性
root.setAttribute("name", "tsu");
// 删除
boolean isRemoved = root.removeChildren("college");
System.err.println(isRemoved);
// 新增
Element newCollege = new Element("college");
newCollege.setAttribute("name", "new_college");
Element newClass = new Element("class");
newClass.setAttribute("name", "ccccc");
newCollege.addContent(newClass);
root.addContent(newCollege);
XMLOutputter out = new XMLOutputter();
File file = new File("src/jdom-modify.xml");
if (file.exists()) {
file.delete();
}
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
out.output(doc, fos);
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
4、【DOM4j】
package test.xml;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.ProcessingInstruction;
import org.dom4j.VisitorSupport;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
/**
* Dom4j读写xml
* @author whwang
*/
public class TestDom4j {
public static void main(String[] args) {
read1();
//read2();
//write();
}
public static void read1() {
try {
SAXReader reader = new SAXReader();
InputStream in = TestDom4j.class.getClassLoader().getResourceAsStream("test.xml");
Document doc = reader.read(in);
Element root = doc.getRootElement();
readNode(root, "");
} catch (DocumentException e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static void readNode(Element root, String prefix) {
if (root == null) return;
// 获取属性
List attrs = root.attributes();
if (attrs != null && attrs.size() > 0) {
System.err.print(prefix);
for (Attribute attr : attrs) {
System.err.print(attr.getValue() + " ");
}
System.err.println();
}
// 获取他的子节点
List childNodes = root.elements();
prefix += "\t";
for (Element e : childNodes) {
readNode(e, prefix);
}
}
public static void read2() {
try {
SAXReader reader = new SAXReader();
InputStream in = TestDom4j.class.getClassLoader().getResourceAsStream("test.xml");
Document doc = reader.read(in);
doc.accept(new MyVistor());
} catch (DocumentException e) {
e.printStackTrace();
}
}
public static void write() {
try {
// 创建一个xml文档
Document doc = DocumentHelper.createDocument();
Element university = doc.addElement("university");
university.addAttribute("name", "tsu");
// 注释
university.addComment("这个是根节点");
Element college = university.addElement("college");
college.addAttribute("name", "cccccc");
college.setText("text");
File file = new File("src/dom4j-modify.xml");
if (file.exists()) {
file.delete();
}
file.createNewFile();
XMLWriter out = new XMLWriter(new FileWriter(file));
out.write(doc);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class MyVistor extends VisitorSupport {
public void visit(Attribute node) {
System.out.println("Attibute: " + node.getName() + "="
+ node.getValue());
}
public void visit(Element node) {
if (node.isTextOnly()) {
System.out.println("Element: " + node.getName() + "="
+ node.getText());
} else {
System.out.println(node.getName());
}
}
@Override
public void visit(ProcessingInstruction node) {
System.out.println("PI:" + node.getTarget() + " " + node.getText());
}
}
package test.xml;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class TestXPath {
public static void main(String[] args) {
read();
}
public static void read() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
InputStream in = TestXPath.class.getClassLoader().getResourceAsStream("test.xml");
Document doc = builder.parse(in);
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
// 选取所有class元素的name属性
// XPath语法介绍: http://w3school.com.cn/xpath/
XPathExpression expr = xpath.compile("//class/@name");
NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println("name = " + nodes.item(i).getNodeValue());
}
} catch (XPathExpressionException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
六、【补充】
注意4种解析方法对TextNode(文本节点)的处理:
1、在使用DOM时,调用node.getChildNodes()获取该节点的子节点,文本节点也会被当作一个Node来返回,如:
package test.xml;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
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.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* dom读写xml
* @author whwang
*/
public class TestDom2 {
public static void main(String[] args) {
read();
}
public static void read() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = dbf.newDocumentBuilder();
InputStream in = TestDom2.class.getClassLoader().getResourceAsStream("test.xml");
Document doc = builder.parse(in);
// root
Element root = doc.getDocumentElement();
if (root == null) return;
// System.err.println(root.getAttribute("name"));
// all college node
NodeList collegeNodes = root.getChildNodes();
if (collegeNodes == null) return;
System.err.println("university子节点数:" + collegeNodes.getLength());
System.err.println("子节点如下:");
for(int i = 0; i < collegeNodes.getLength(); i++) {
Node college = collegeNodes.item(i);
if (college == null) continue;
if (college.getNodeType() == Node.ELEMENT_NODE) {
System.err.println("\t元素节点:" + college.getNodeName());
} else if (college.getNodeType() == Node.TEXT_NODE) {
System.err.println("\t文本节点:" + Arrays.toString(college.getTextContent().getBytes()));
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出的结果是:
university子节点数:3
子节点如下:
文本节点:[10, 9]
元素节点:college
文本节点:[10]
其中\n的ASCII码为10,\t的ASCII码为9。结果让人大吃一惊,university的子节点数不是1,也不是2,而是3,这3个子节点都是谁呢?为了看得更清楚点,把xml文档改为:
11
22
还是上面的程序,输出结果为:
university子节点数:3
子节点如下:
文本节点:[49, 49, 10, 9]
元素节点:college
文本节点:[50, 50, 10]
其中数字1的ASCII码为49,数字2的ASCII码为50。
2、使用SAX来解析同DOM,当你重写它的public void characters(char[] ch, int start, int length)方法时,你就能看到。
3、JDOM,调用node.getChildren()只返回子节点,不包括TextNode节点(不管该节点是否有Text信息)。如果要获取该节点的Text信息,可以调用node.getText()方法,该方法返回节点的Text信息,也包括\n\t等特殊字符。
4、DOM4j同JDOM
参考:
http://www.docin.com/p-78963650.html
http://wenku.baidu.com/view/b091f9360b4c2e3f5727638b.html
http://kree.iteye.com/blog/668280