dom4j是一个Java的XML API,类似于jdom,用来读写XML文件
dom4j的使用方法简单总结来说如下:
①可以创建一个新的xml文件
②利用SAXReader和File对象创建一个已存在的xml文件的一个Document对象
③利用Document对象的getRootElement()方法获取根节点,返回值类型为Element
④利用根节点,可以用迭代器遍历子节点,也可以直接利用XPATH语法查找节点,对节点元素、属性读取或更改
⑤将更改写入xml文件保存
下面来看简单的实例:
①创建一个新的xml文件,这是dom4j官方文档中的一个例子
1 import org.dom4j.Document; 2 import org.dom4j.DocumentHelper; 3 import org.dom4j.Element; 4 5 public class Foo { 6 7 public Document createDocument() { 8 Document document = DocumentHelper.createDocument(); 9 Element root = document.addElement( "root" ); 10 11 Element author1 = root.addElement( "author" ) 12 .addAttribute( "name", "James" ) 13 .addAttribute( "location", "UK" ) 14 .addText( "James Strachan" ); 15 16 Element author2 = root.addElement( "author" ) 17 .addAttribute( "name", "Bob" ) 18 .addAttribute( "location", "US" ) 19 .addText( "Bob McWhirter" ); 20 21 return document; 22 } 23 }
②利用SAXReader和File对象或xml的URL创建一个已存在的xml文件的一个Document对象
这里是dom4j官方文档中利用xml文件URL创建Document对象的一个例子:
1 import java.net.URL; 2 3 import org.dom4j.Document; 4 import org.dom4j.DocumentException; 5 import org.dom4j.io.SAXReader; 6 7 public class Foo { 8 9 public Document parse(URL url) throws DocumentException { 10 SAXReader reader = new SAXReader(); 11 Document document = reader.read(url); 12 return document; 13 } 14 }
下面是我写的用SAXReader和File创建Document对象的例子:
1 import java.io.File; 2 import java.io.IOException; 3 4 import org.dom4j.Document; 5 import org.dom4j.DocumentException; 6 import org.dom4j.io.SAXReader; 7 8 public class Foo { 9 10 public Document parse() throws DocumentException ,IOException{ 11 SAXReader reader = new SAXReader(); 12 File file = new File("Student.xml"); 13 Document document = reader.read(file); 14 return document; 15 } 16 }
③利用Document对象的getRootElement()方法获取根节点,返回值类型为Element
1 Element rootElement = document.getRootElement();
④用迭代器遍历子节点,也可以直接利用XPATH语法查找节点,对节点元素、属性读取或更改
利用迭代器遍历:
1 public void bar(Document document) throws DocumentException { 2 3 Element root = document.getRootElement(); 4 5 //迭代root的子节点 6 for ( Iterator i = root.elementIterator(); i.hasNext(); ) { 7 Element element = (Element) i.next(); 8 // do something 9 } 10 11 // 迭代root的名为"foo"的子节点 12 for ( Iterator i = root.elementIterator( "foo" ); i.hasNext(); ) { 13 Element foo = (Element) i.next(); 14 // do something 15 } 16 17 // 迭代root的属性 18 for ( Iterator i = root.attributeIterator(); i.hasNext(); ) { 19 Attribute attribute = (Attribute) i.next(); 20 // do something 21 } 22 }
利用XPATH语法查找节点:
1 public void bar(Document document) { 2 List list = document.selectNodes( "//foo/bar" ); 3 4 Node node = document.selectSingleNode( "//foo/bar/author" ); 5 6 String name = node.valueOf( "@name" ); 7 } 8 9 public void findLinks(Document document) throws DocumentException { 10 11 List list = document.selectNodes( "//a/@href" ); 12 13 for (Iterator iter = list.iterator(); iter.hasNext(); ) { 14 Attribute attribute = (Attribute) iter.next(); 15 String url = attribute.getValue(); 16 } 17 }
1 import org.dom4j.Document; 2 import org.dom4j.io.OutputFormat; 3 import org.dom4j.io.XMLWriter; 4 5 public class Foo { 6 7 public void write(Document document) throws IOException { 8 9 // lets write to a file 10 XMLWriter writer = new XMLWriter( 11 new FileWriter( "output.xml" ) 12 ); 13 writer.write( document ); 14 writer.close(); 15 16 17 // Pretty print the document to System.out 18 OutputFormat format = OutputFormat.createPrettyPrint(); 19 writer = new XMLWriter( System.out, format ); 20 writer.write( document ); 21 22 // Compact format to System.out 23 format = OutputFormat.createCompactFormat(); 24 writer = new XMLWriter( System.out, format ); 25 writer.write( document ); 26 } 27 }