Dom4j是一个易于使用的,开源的库,在Java平台上与XML,XPath,XSLT协同工作。使用Java集合框架,全面支持DOM,SAX,JAXP。
官方网站:http://dom4j.org
1.将XML文件转换为一个Document对象
import java.net.URL;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
public class Foo {
public Document parse(URL url) throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(url);
return document;
}
}
2.很多方法用于操作Document,可以返回标准的Java迭代器
public void bar(Document document) throws DocumentException {
Element root = document.getRootElement();
// iterate through child elements of root
for ( Iterator i = root.elementIterator(); i.hasNext(); ) {
Element element = (Element) i.next();
// do something
}
// iterate through child elements of root with element name "foo"
for ( Iterator i = root.elementIterator( "foo" ); i.hasNext(); ) {
Element foo = (Element) i.next();
// do something
}
// iterate through attributes of root
for ( Iterator i = root.attributeIterator(); i.hasNext(); ) {
Attribute attribute = (Attribute) i.next();
// do something
}
}
3.快速循环
如果你需要遍历一个很大的XML文档,可以使用快速循环(递归)来改善性能。可以避免为每一次循环都创建一个迭代器对象。
public void treeWalk(Document document) {
treeWalk( document.getRootElement() );
}
public void treeWalk(Element element) {
for ( int i = 0, size = element.nodeCount(); i < size; i++ ) {
Node node = element.node(i);
if ( node instanceof Element ) {
treeWalk( (Element) node );
}
else {
// do something....
}
}
}
4.创建一个新的XML文档
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class Foo {
public Document createDocument() {
Document document = DocumentHelper.createDocument();
Element root = document.addElement( "root" );
Element author1 = root.addElement( "author" )
.addAttribute( "name", "James" )
.addAttribute( "location", "UK" )
.addText( "James Strachan" );
Element author2 = root.addElement( "author" )
.addAttribute( "name", "Bob" )
.addAttribute( "location", "US" )
.addText( "Bob McWhirter" );
return document;
}
}
5.将XML文档写入文件
通过write()方法将一个XML文档写入文件是最简单的方式。
FileWriter out = new FileWriter( "foo.xml" );
document.write( out );
如果你想改变输出的格式,比如美观的格式(含缩进)和压缩的格式(不含缩进),可以使用XMLWriter类。
import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class Foo {
public void write(Document document) throws IOException {
// lets write to a file
XMLWriter writer = new XMLWriter(
new FileWriter( "output.xml" )
);
writer.write( document );
writer.close();
// Pretty print the document to System.out
OutputFormat format = OutputFormat.createPrettyPrint();
writer = new XMLWriter( System.out, format );
writer.write( document );
// Compact format to System.out
format = OutputFormat.createCompactFormat();
writer = new XMLWriter( System.out, format );
writer.write( document );
}
}
6.XML和String之间的相互转换
通过asXML()方法,你可以将一个Document,Attribute或Element对象转换成一个包含XML文本的字符串。
Document document = ...;
String text = document.asXML();
同样,通过DocumentHelper.parseText()方法,你也可以方便地将一个字符串形式的XML转换成一个Document对象。
String text = "
Document document = DocumentHelper.parseText(text);