The Code is as follows:
In addition to the dom4j jar package, you also need the jar package: 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. extends entexception; 11 import Org. dom4j. io. saxreader; 12 13 public class dom4jmodifyxml {14 public void modifydocument (File inputxml) {15 try {16 // use saxreader to parse the XML document for 17 S Axreader saxreader = new saxreader (); 18 document = saxreader. Read (inputxml); 19 20 // obtain the level node list from the article element using the XPath expression. 21 // if the level attribute value is intermediate, change it to introductory. 22 // locate the node using the jar package: jaxen-1.1-beta-6.jar23 list = document. selectnodes ("// article/@ level"); 24 iterator iter = List. iterator (); 25 while (ITER. hasnext () {26 attribute = (attribute) ITER. next (); 27 if (attribute. getvalue (). equals ("intermediate") 28 attribute. setvalue ("Introductory"); 29} 30 31 // Date modified 32 list = document. selectnodes ("// article/@ date"); 33 iter = List. iterator (); 34 while (ITER. Hasnext () {35 attribute = (attribute) ITER. next (); 36 IF (attribute. getvalue (). equals ("December-2001") 37 attribute. setvalue ("October-2002"); 38} 39 40 // gets the list of Article elements, obtains an iterator from the title element in the article element, and modifies the text of the title element. 41 list = document. selectnodes ("// article"); 42 iter = List. iterator (); 43 while (ITER. hasnext () {44 element = (element) ITER. next (); 45 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 // modify the author's surname 54 list = document. selectnodes ("// article/author"); 55 iter = List. iterator (); 56 while (ITER. hasnext () {57 element = (element) ITER. next (); 58 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 // modify the author's name 67 list = document. selectnodes ("// article/author"); 68 iter = List. iterator (); 69 while (ITER. hasnext () {70 element = (element) ITER. next (); 71 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 // write the modified file stream to the New XML file. 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 (incluentexception 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}
The original catalog. xml file:
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>
Modified catalog-modify.xml File
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>
Use dom4j to modify an XML file