dom4j是一個Java的XML API,相似於jdom,用來讀寫XML文件的。dom4j是一個十分優秀的JavaXML API,具備性能優異、功能強大和極其易使用的特色,同時它也是一個開放源代碼的軟件,能夠在SourceForge上找到它。在IBM developerWorks上面還能夠找到一篇文章,對主流的Java XML API進行的性能、功能和易用性的評測,因此能夠知道dom4j不管在哪一個方面都是很是出色的。現在能夠看到愈來愈多的Java軟件都在使用dom4j來讀寫XML,特別值得一提的是連Sun的JAXM也在用dom4j。這已是必須使用的jar包, Hibernate也用它來讀寫配置文件。node
具體操做以下:
<?xml version="1.0" encoding="UTF-8"?>
<msg>
<content>
<name>xiaohua</name>
<address>china</address>
</content>
<other>
<car>
<carCode>99999</carCode>
<carNo xmlns:jsdl="http://www.ibm.com/xmlns/prod/scheduling/1.0/jsdl"
xmlns:jsdlws="http://www.ibm.com/xmlns/prod/scheduling/1.0/jsdlws"><![CDATA[<author>xiaoming</author>]]
></carNo>
</car>
</other>
</msg>
/**
* 組裝XML
*/
public static void CreateXML(){
Document document = DocumentHelper.createDocument();
Element rootElement = document.addElement("msg");
Element contentElement = rootElement.addElement("content");
Element verElement = contentElement.addElement("name");
verElement.addText("xiaohua");
Element addressElement = contentElement.addElement("address");
addressElement.addText("china");
Element otherElement = rootElement.addElement("other");
Element carElement = otherElement.addElement("car");
Element taxOrgCodeElement = carElement.addElement("carCode");
taxOrgCodeElement.addText("99999");
Element carNoElement = carElement.addElement("carNo");
carNoElement.addNamespace("jsdl", "http://www.ibm.com/xmlns/prod/scheduling/1.0/jsdl");
carNoElement.addNamespace("jsdlws", "http://www.ibm.com/xmlns/prod/scheduling/1.0/jsdlws");
String cdataString = "<author>xiaoming</author>";
carNoElement.addCDATA(cdataString);
//文件輸出
OutputFormat outputFormat = null;
try {
String fileName = "D:\\tmp\\test.xml";
outputFormat = OutputFormat.createPrettyPrint();
outputFormat.setEncoding("UTF-8");
XMLWriter xw = new XMLWriter(new FileOutputStream(fileName),outputFormat);
outputFormat.setNewLineAfterDeclaration(false);
xw.write(document);
xw.close();
} catch (Exception e) {
System.out.println(e);
}
}dom
/**
* 讀取XML
*/
public static void readXML(){
try {
String filePath = "D:\\tmp\\test.xml";
SAXReader reader = new SAXReader();
Document document = reader.read(new File(filePath));
Element msgElement = document.getRootElement();
Element contentElement = msgElement.element("content");
Element varElement = contentElement.element("name");
String name = varElement.getText();
System.out.println("名字:"+name);
Element otherElement = msgElement.element("other");
Element carHeadElement = otherElement.element("car");
Element taxorgCodeElement =carHeadElement.element("carCode");
System.out.println("carCode:"+taxorgCodeElement.getText());
Element carNoElemnet = carHeadElement.element("carNo");
System.out.println("carNo:"+carNoElemnet.getText());
Element documents = (Element)document.selectNodes("msg/other/car/carNo").get(0);
String s = documents.getStringValue();
System.out.println("s:"+s);
Document documentt = DocumentHelper.parseText(s);
Node node = documentt.selectSingleNode("author");
System.out.println("CDATA重的內容:"+node.getStringValue());
} catch (Exception e) {
System.out.println(e);
}
}性能