1 package org.zzn.dom4j;
2
3 import org.dom4j.Document;
4 import org.dom4j.Element;
5 import org.dom4j.Attribute;
6 import java.util.List;
7 import java.util.Iterator;
8
9 import org.dom4j.io.OutputFormat;
10 import org.dom4j.io.XMLWriter;
11 import java.io.*;
12 import org.dom4j.DocumentException;
13 import org.dom4j.io.SAXReader;
14
15
16 public class Dom4JParser{
17 public void modifyDocument(File inputXml){
18 try{
19 //使用 SAXReader 解析 XML 文档 catalog.xml
20 SAXReader saxReader = new SAXReader();
21 Document document = saxReader.read(inputXml);
22
23 //SAXReader 包含在 org.dom4j.io 包中。
24 //inputXml 是从 c:/catalog/catalog.xml 创建的 java.io.File。
25 //使用 XPath 表达式从 article 元素中获得 level 节点列表。
26 //如果 level 属性值是“Intermediate”则改为“Introductory”。
27 List list = document.selectNodes("//article/@level" );
28 Iterator iter=list.iterator();
29 while(iter.hasNext()){
30 Attribute attribute=(Attribute)iter.next();
31 if(attribute.getValue().equals("Intermediate"))
32 attribute.setValue("Introductory");
33 }
34
35 //获取 article 元素列表,从 article 元素中的 title 元素得到一个迭代器,并修改 title 元素的文本。
36 list = document.selectNodes("//article/@date" );
37 iter=list.iterator();
38 while(iter.hasNext()){
39 Attribute attribute=(Attribute)iter.next();
40 if(attribute.getValue().equals("December-2001"))
41 attribute.setValue("October-2002");
42 }
43 list = document.selectNodes("//article" );
44 iter=list.iterator();
45 while(iter.hasNext()){
46 Element element=(Element)iter.next();
47 Iterator iterator=element.elementIterator("title");
48 while(iterator.hasNext()){
49 Element titleElement=(Element)iterator.next();
50 if(titleElement.getText().equals("Java configuration with XML Schema"))
51 titleElement.setText("Create flexible and extensible XML schema");
52 }
53 }
54
55 //通过和 title 元素类似的过程修改 author 元素
56 list = document.selectNodes("//article/author" );
57 iter=list.iterator();
58 while(iter.hasNext()){
59 Element element=(Element)iter.next();
60 Iterator iterator=element.elementIterator("firstname");
61 while(iterator.hasNext()){
62 Element firstNameElement=(Element)iterator.next();
63 if(firstNameElement.getText().equals("Marcello"))
64 firstNameElement.setText("Ayesha");
65 }
66 }
67 list = document.selectNodes("//article/author" );
68 iter=list.iterator();
69 while(iter.hasNext()){
70 Element element=(Element)iter.next();
71 Iterator iterator=element.elementIterator("lastname");
72 while(iterator.hasNext()){
73 Element lastNameElement=(Element)iterator.next();
74 if(lastNameElement.getText().equals("Vitaletti"))
75 lastNameElement.setText("Malik");
76 }
77 }
78
79 OutputFormat format = OutputFormat.createPrettyPrint();
80 format.setEncoding("GBK");
81 XMLWriter output = new XMLWriter(
82 new FileWriter( new File("e:/catalog-modified.xml") ),format);
83 output.write( document );
84 output.close();
85 }
86
87 catch(DocumentException e)
88 {
89 System.out.println(e.getMessage());
90 }
91 catch(IOException e){
92 System.out.println(e.getMessage());
93 }
94 }
95 public static void main(String[] argv){
96 Dom4JParser dom4jParser=new Dom4JParser();
97 dom4jParser.modifyDocument(new File("e:/catalog.xml"));
98 }
99 }
100
运行此代码需要 dom4j.jar包和jaxen.jar包