dom4j是一个Java的XML API,类似于jdom,用来读写XML文件
dom4j的使用方法简单总结来说如下:
①可以创建一个新的xml文件
②利用SAXReader和File对象创建一个已存在的xml文件的一个Document对象
③利用Document对象的getRootElement()方法获取根节点,返回值类型为Element
④利用根节点,可以用迭代器遍历子节点,也可以直接利用XPATH语法查找节点,对节点元素、属性读取或更改
⑤将更改写入xml文件保存
下面来看简单的实例:
①创建一个新的xml文件,这是dom4j官方文档中的一个例子
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; } }
②利用SAXReader和File对象或xml的URL创建一个已存在的xml文件的一个Document对象
这里是dom4j官方文档中利用xml文件URL创建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; } }
下面是我写的用SAXReader和File创建Document对象的例子:
import java.io.File; import java.io.IOException; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.SAXReader; public class Foo { public Document parse() throws DocumentException ,IOException{ SAXReader reader = new SAXReader(); File file = new File("Student.xml"); Document document = reader.read(file); return document; } }
Element rootElement = document.getRootElement();
④用迭代器遍历子节点,也可以直接利用XPATH语法查找节点,对节点元素、属性读取或更改
利用迭代器遍历:
public void bar(Document document) throws DocumentException { Element root = document.getRootElement(); //迭代root的子节点 for ( Iterator i = root.elementIterator(); i.hasNext(); ) { Element element = (Element) i.next(); // do something } // 迭代root的名为"foo"的子节点 for ( Iterator i = root.elementIterator( "foo" ); i.hasNext(); ) { Element foo = (Element) i.next(); // do something } // 迭代root的属性 for ( Iterator i = root.attributeIterator(); i.hasNext(); ) { Attribute attribute = (Attribute) i.next(); // do something } }
public void bar(Document document) { List list = document.selectNodes( "//foo/bar" ); Node node = document.selectSingleNode( "//foo/bar/author" ); String name = node.valueOf( "@name" ); } public void findLinks(Document document) throws DocumentException { List list = document.selectNodes( "//a/@href" ); for (Iterator iter = list.iterator(); iter.hasNext(); ) { Attribute attribute = (Attribute) iter.next(); String url = attribute.getValue(); } }
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 ); } }
博客园博文地址:dom4j 的使用