Although Java already has two standard parsing methods, Dom and sax
However, it is not easy to operate. For a beginner like me, some of themCodeIs a real disgusting
To this end, great third-party developers have developed tools such as JDOM and dom4j.
In view of the current trend, let's talk about the basic usage of dom4j, and do not involve complex operations such as recursion.
Dom4j is widely used. The examples on the official website are so obscure that I will not write them here.
First, we need to create an XML document before parsing it.
XML document:
<? XML version = "1.0" encoding = "UTF-8"?> <Books> <book id = "001"> <title> Harry Potter </title> <author> j k. rowling </author> </book> <book id = "002"> <title> learning XML </title> <author> Erik T. ray </author> </book> </books>
Example 1: Parse XML using list
Import Java. io. file; import Java. util. list; import Org. dom4j. attribute; import Org. dom4j. document; import Org. dom4j. element; import Org. dom4j. io. saxreader; public class demo {public static void main (string [] ARGs) throws exception {saxreader reader = new saxreader (); file = new file ("books. XML "); document = reader. read (File); element root = document. getrootelement (); List <element> childelements = root. elements (); For (element child: childelements) {// unknown attribute name/* List <attribute> attributelist = child. attributes (); For (attribute ATTR: attributelist) {system. out. println (ATTR. getname () + ":" + ATTR. getvalue ();} * // system with a known attribute name. out. println ("ID:" + child. attributevalue ("ID"); // If the sub-element name is unknown,/* List <element> elementlist = child. elements (); For (element ele: elementlist) {system. out. println (Ele. getname () + ":" + ELE. gettext ();} system. out. println (); * // when the sub-element name is known, system. out. println ("title" + child. elementtext ("title"); system. out. println ("author" + child. elementtext ("author"); // This line exists for the purpose of formatting. out. println ();}}}
Example 2: Use the iterator to parse XML
Import Java. io. file; import Java. util. iterator; import Org. dom4j. attribute; import Org. dom4j. document; import Org. dom4j. element; import Org. dom4j. io. saxreader; public class demo {public static void main (string [] ARGs) throws exception {saxreader reader = new saxreader (); document = reader. read (new file ("books. XML "); element root = document. getrootelement (); iterator it = root. elementiterator (); While (it. hasnext () {element = (element) it. next (); // when the attribute name is unknown/* iterator attrit = element. attributeiterator (); While (attrit. hasnext () {attribute a = (attribute) attrit. next (); system. out. println (. getvalue ();} * // system with a known attribute name. out. println ("ID:" + element. attributevalue ("ID"); // If the element name is unknown,/* iterator eleit = element. elementiterator (); While (eleit. hasnext () {Element E = (element) eleit. next (); system. out. println (E. getname () + ":" + E. gettext ();} system. out. println (); * // If the element name is known, system. out. println ("title:" + element. elementtext ("title"); system. out. println ("Author:" + element. elementtext ("author"); system. out. println ();}}}
Running result:
Example 3: Create an XML document and output it to a file
Import Java. io. file; import Java. io. fileoutputstream; import Org. dom4j. document; import Org. dom4j. export enthelper; import Org. dom4j. element; import Org. dom4j. io. outputformat; import Org. dom4j. io. xmlwriter; public class demo {public static void main (string [] ARGs) throws exception {document DOC = documenthelper. createdocument (); // Add the root node element books = Doc. addelement ("books"); // Add the sub-element book1 = books. addelement ("book"); element title1 = book1.addelement ("title"); element author1 = book1.addelement ("author"); element book2 = books. addelement ("book"); element title2 = book2.addelement ("title"); element author2 = book2.addelement ("author"); // Add a property book1.addattriement ("ID ", "001"); // Add title1.settext ("Harry Potter") for the element; author1.settext ("j k. rowling "); book2.addattribute (" ID "," 002 "); title2.settext (" Learning XML "); author2.settext (" Erik T. ray "); // instantiate the output format object outputformat format = outputformat. createprettyprint (); // sets the output encoding format. setencoding ("UTF-8"); // create the file object file = new file ("D:" + file. separator + "books. XML "); // generate the xmlwriter object. The parameters in the constructor are the file stream to be output and the format xmlwriter writer = new xmlwriter (New fileoutputstream (file), format ); // start writing. The write method contains the Document Object writer created above. write (DOC );}}
Running result: