转化XML
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;
}
}
使用迭代器
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
}
}
快速循环
快速遍历大文件:
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....
}
}
}
创建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;
}
}
将文档写入文件
一种快速而且简单的方式如下:
FileWriter out = new FileWriter( "foo.xml" );
document.write( out );
如果想要修改输出的格式,如智能缩进输出或者紧凑输出,或者你想要使用writer
对象或者OutputStream
对象来完成其他任务,则可以使用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 );
}
}
与String的转化
如果有到Document
或者其他结点如Attribute
、Element
等的引用,则可以用asXML()
方法将其转化为默认的XML文本。
Document document = ...;
String text = document.asXML();
如果有String
形式的XML,则可以使用DocumentHelper.parseText()
将其转化为Document
。
String text = "<person> <name>James</name> </person>";
Document document = DocumentHelper.parseText(text);
其他操作
1 取得某结点下的某个子节点或属性
Element root = document.getRootElement();
Element elem = root.element("book");
Attribute attribute = root.attribute("id");
2 取得属性的内容
String text = attribute.getText();
3 删除某属性