dom4j操作xml

  dom4j是一个Java的XML API,类似于jdom,用来读写XML文件。是一个非常优秀的Java XML API,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源工具。可以在这个地址http://dom4j.sourceforge.net进行下载。

  这里我们使用到的dom4j是dom4j-1.6.1这个版本,我们只需要使用到如下两个jar包:

  dom4j-1.6.1.jar

  commons-io-2.4.jar

1、dom4j读取xml字符串

 1 import org.dom4j.Document;
 2 import org.dom4j.DocumentException;
 3 import org.dom4j.DocumentHelper;
 4 import org.dom4j.Element;
 5 
 6 public class TestReadXMLString {
 7     public static void main(String[] args) throws DocumentException {
 8         String readline = "<?xml version=\"1.0\" encoding=\"utf-8\"?><students><student sid=\"001\"> <id>001</id><name>灰机</name> <age>18</age> </student></students>";
9 Document document = DocumentHelper.parseText(readline);// 加载字符串获得文档对象 10 Element rootElm = document.getRootElement();//获得文档根节点 11 System.out.println("rootElement: " + rootElm.getName());//打印根节点的名称 12 Element student = rootElm.element("student"); 13 Element id = student.element("id"); 14 Element name = student.element("name"); 15 Element age = student.element("age"); 16 System.out.println(id.getText()); 17 System.out.println(name.getText()); 18 System.out.println(age.getText()); 19 } 20 }

 

 2、dom4j创建xml文件

 1 import org.dom4j.Document;
 2 import org.dom4j.DocumentHelper;
 3 import org.dom4j.Element;
 4 import org.dom4j.io.OutputFormat;
 5 public class TestWriteXMLString {
 6     public static void main(String[] args) {
 7         OutputFormat format = OutputFormat.createPrettyPrint();
 8         // 1. 构造空的Document
 9         Document doc = DocumentHelper.createDocument();
10         doc.addComment("this is a comment");
11         // 2. 构造根元素
12         Element rootElmt = doc.addElement("users");
13         rootElmt.addNamespace("test", "www.test.com");
14 
15         Element userElmt = rootElmt.addElement("user");
16         userElmt.addAttribute("number", "1001");
17         userElmt.addElement("name").setText("zhangsan");
18         userElmt.addElement("age").setText("20");
19         userElmt.addElement("gender").setText("mail");
20 
21         Element userElmt2 = rootElmt.addElement("user");
22         userElmt.addAttribute("number", "1002");
23         userElmt2.addElement("name").setText("zhangsan");
24         userElmt2.addElement("age").setText("20");
25         userElmt2.addElement("gender").setText("mail");
26 
27         System.out.println(doc.asXML().replaceAll("\n", ""));
28     }
29 }

 

 3、读取或写xml文件

  读取xml文件

 1 SAXReader reader = new SAXReader();
 2       String path = "E:/Workspaces/MyEclipse 8.6/xmltest/file/student.xml";
 3       Document document = reader.read(new File(path));
 4 
 5   写xml文件
 6 
 7   OutputFormat format = OutputFormat.createPrettyPrint();
 8   format.setEncoding("utf-8");// 设置XML文件的编码格式
 9 
10   String filePath = "E:/Workspaces/MyEclipse 8.6/xmltest/file/student.xml";
11       Document document = DocumentHelper.createDocument();
12       doc.addComment("this is a comment");
13 
14   //创建document内容。。。
15   XMLWriter writer = new XMLWriter(new FileWriter(filePath), format);//写入指定的文件
16 
17   writer.write(document);
18 
19    writer.close();

posted on 2013-11-28 16:53  烟火_  阅读(571)  评论(0编辑  收藏  举报

导航