代码如下:
需要使用的jar包除了dom4j的jar包之外,还需要jar包:jaxen-1.1-beta-6.jar
1 package xml; 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 import org.dom4j.io.XMLWriter; 9 import java.io.*; 10 import org.dom4j.DocumentException; 11 import org.dom4j.io.SAXReader; 12 13 public class Dom4jModifyXml { 14 public void modifyDocument(File inputXml) { 15 try { 16 //使用 SAXReader 解析 XML 文档 17 SAXReader saxReader = new SAXReader(); 18 Document document = saxReader.read(inputXml); 19 20 //使用 XPath 表达式从 article 元素中获得 level 节点列表。 21 //如果 level 属性值是Intermediate则改为Introductory。 22 //定位节点需要使用jar包:jaxen-1.1-beta-6.jar 23 List list = document.selectNodes("//article/@level"); 24 Iterator iter = list.iterator(); 25 while (iter.hasNext()) { 26 Attribute attribute = (Attribute) iter.next(); 27 if (attribute.getValue().equals("Intermediate")) 28 attribute.setValue("Introductory"); 29 } 30 31 //修改日期 32 list = document.selectNodes("//article/@date"); 33 iter = list.iterator(); 34 while (iter.hasNext()) { 35 Attribute attribute = (Attribute) iter.next(); 36 if (attribute.getValue().equals("December-2001")) 37 attribute.setValue("October-2002"); 38 } 39 40 //获取 article 元素列表,从 article 元素中的 title 元素得到一个迭代器,并修改 title 元素的文本。 41 list = document.selectNodes("//article"); 42 iter = list.iterator(); 43 while (iter.hasNext()) { 44 Element element = (Element) iter.next(); 45 Iterator iterator = element.elementIterator("title"); 46 while (iterator.hasNext()) { 47 Element titleElement = (Element) iterator.next(); 48 if (titleElement.getText().equals("Java configuration with XML Schema")) 49 titleElement.setText("Create flexible and extensible XML schema"); 50 } 51 } 52 53 //修改作者的姓 54 list = document.selectNodes("//article/author"); 55 iter = list.iterator(); 56 while (iter.hasNext()) { 57 Element element = (Element) iter.next(); 58 Iterator iterator = element.elementIterator("firstname"); 59 while (iterator.hasNext()) { 60 Element firstNameElement = (Element) iterator.next(); 61 if (firstNameElement.getText().equals("Marcello")) 62 firstNameElement.setText("Ayesha"); 63 } 64 } 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 //将修改后的文档流写入新的xml文件中 80 XMLWriter output = new XMLWriter(new FileWriter(new File("E:/xml/catalog-modify.xml"))); 81 output.write(document); 82 output.close(); 83 } 84 85 catch (DocumentException e) { 86 System.out.println(e.getMessage()); 87 } catch (IOException e) { 88 System.out.println(e.getMessage()); 89 } 90 } 91 92 public static void main(String[] argv) { 93 Dom4jModifyXml dom4jModifyXml = new Dom4jModifyXml(); 94 dom4jModifyXml.modifyDocument(new File("E:/xml/catalog.xml")); 95 } 96 }
原来的catalog.xml文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <catalog> 3 <!--An XML Catalog--> 4 <?target instruction?> 5 <journal title="XML Zone" publisher="IBM developerWorks"> 6 <article level="Intermediate" date="December-2001"> 7 <title>Java configuration with XML Schema</title> 8 <author> 9 <firstname>Marcello</firstname> 10 <lastname>Vitaletti</lastname> 11 </author> 12 </article> 13 </journal> 14 </catalog>
修改后的catalog-modify.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <catalog> 3 <!--An XML Catalog--> 4 <?target instruction?> 5 <journal title="XML Zone" publisher="IBM developerWorks"> 6 <article level="Introductory" date="October-2002"> 7 <title>Create flexible and extensible XML schema</title> 8 <author> 9 <firstname>Ayesha</firstname> 10 <lastname>Malik</lastname> 11 </author> 12 </article> 13 </journal> 14 </catalog>
时间: 2024-10-18 07:35:01