使用dom4j操作XML

  DOM4J介绍

  DOM4J是使用Java语言编写的,用于读写及操作XML的一套组件,DOM4J同时具有DOM修改文件的优点和SAX读取快速的优点。 

  DOM4J的使用

  首先下载dom4j的JAR包,我用的版本是1.6.1,下载地址:http://download.csdn.net/detail/onroads/9721574

  使用方法《使用JDOM操作XML》,链接:http://www.cnblogs.com/centor/p/6215082.html

  另外,解压后用到dom4j01.6.1.jar文件,还有一个lib/jaxen-1.1-beta-6.jar文件,一般也需要引入,否则执行时可能抛出java.lang.NoClassDefFoundError:org/jaxen/JaxenException异常。DOM4J的主要接口如下:

  使用DOM4J

   生成xml例程:

 1 package com.xml;
 2 
 3 import java.io.File;
 4 import java.io.FileOutputStream;
 5 
 6 import org.dom4j.Attribute;
 7 import org.dom4j.Document;
 8 import org.dom4j.DocumentHelper;
 9 import org.dom4j.Element;
10 import org.dom4j.io.OutputFormat;
11 import org.dom4j.io.XMLWriter;
12 
13 
14 public class XMLtest {
15 
16     public static void main(String args[]) throws Exception {
17 
18         String[] names = {"鸣人","佐助","小樱"};
19         String[] emails = {"mingren@163.com","zuozhu@163.com","xiaoying@163.com"};
20         String[] ids ={"H1","H2","H3"};
21         
22         Document doc = DocumentHelper.createDocument();
23         Element addresslist = doc.addElement("addresslist");    
24         
25         for (int i = 0; i < 3; i++) {
26             Element linkman = addresslist.addElement("linkman");
27             Element name = linkman.addElement("name");
28             Element email = linkman.addElement("email");
29             name.setText(names[i]);
30             email.setText(emails[i]);
31             name.setAttributeValue("id", ids[i]);    
32         }
33         
34         OutputFormat format = OutputFormat.createPrettyPrint();
35         format.setEncoding("GBK");
36         try {
37             XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("D:"+File.separator+"person.xml"), format);
38             xmlWriter.write(doc);
39             xmlWriter.close();
40         } catch (Exception e) {
41             e.printStackTrace();
42         }
43     }
44 }

  生成的xml文件:

  

 

 

 

 

 

 

 

 

 

解析XML文件:

 

 1 package com.xml;
 2 
 3 import java.io.File;
 4 import java.util.Iterator;
 5 
 6 import org.dom4j.Document;
 7 import org.dom4j.Element;
 8 import org.dom4j.io.SAXReader;
 9 
10 public class XMLtest {
11 
12     public static void main(String args[]) throws Exception {
13 
14         File file = new File("D:"+File.separator+"person.xml");
15         SAXReader reader = new SAXReader();
16         Document doc = null;
17         try {
18             doc = reader.read(file);
19             
20         } catch (Exception e) {
21             e.printStackTrace();
22         }
23         
24         Element root = doc.getRootElement();
25         Iterator iterator = root.elementIterator();
26         while(iterator.hasNext()){
27             System.out.println("<linkman>");
28             Element linkman = (Element) iterator.next();
29             System.out.println("  <name id="+ linkman.element("name").attributeValue("id")+">"+linkman.elementText("name")+"</name>");
30             System.out.println("  <email>"+linkman.elementText("email")+"</email>");
31             System.out.println("</linkman>");
32         }
33     }
34 }

解析结果:

 

posted @ 2016-12-26 17:25  风雪夜归人shen  阅读(392)  评论(0编辑  收藏  举报