1, Jdom analysis
First, import the additional jar packages:
Build Path:jdom-2.0.6.jar
Prepare to get a collection of child nodes:
PackageCom.imooc_xml.jdom.text;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.util.List;Importorg.jdom2.Document;Importorg.jdom2.Element;Importorg.jdom2.JDOMException;ImportOrg.jdom2.input.SAXBuilder; Public classJdomtest { Public Static voidMain (string[] args) {//1. Create a Saxbuilder objectSaxbuilder Saxbuilder =NewSaxbuilder (); InputStream in; Try {//2. Create an input stream to load the XML file into the input streamin =NewFileInputStream ("Xml/books.xml");//3. Load the input stream into Saxbuilder via the Saxbuilder build methodDocument document =Saxbuilder.build (in);//4. Get the root node of the XML file through the Document objectElement rootelement =document.getrootelement ();//5. Gets the list collection of child nodes under the root nodeList<element> Booklist =Rootelement.getchildren (); } Catch(FileNotFoundException e) {e.printstacktrace (); }Catch(Jdomexception |IOException E) {E.printstacktrace (); } }}
Gets the attribute name and attribute value of the node, the section name and value:
for(Element book:booklist) {System.out.println ("Start parsing" + (Booklist.indexof +1) + "book"); List<Attribute> attrlist =book.getattributes (); for(Attribute attr:attrlist) {attr.getname (); Attr.getvalue (); } //book.getattributevalue ("id");List<Element> Bookchildren =Book.getchildren (); for(Element children:bookchildren) {children.getname (); Children.getvalue (); } }
Garbled processing:
Use InputStreamReader () to specify the encoding
Storing book objects in Jdom
Private StaticArraylist<book> Booklist =NewArraylist<book>();//for further analysis for(Element book:booklist) {book bookentity=NewBook (); for(Attribute attr:attrlist) {String attrname=Attr.getname (); String AttrValue=Attr.getvalue (); if(Attrname.equals ("id") {Bookentity.setid (attrValue); } } for(Element children:bookchildren) {String name=Children.getname (); String value=Children.getvalue (); if(Name.equals ("name") {bookentity.setname (value); }Else if(Name.equals ("Author") {bookentity.setauthor (value); }Else if(Name.equals ("Year") {bookentity.setyear (value); }Else if(Name.equals ("Price") {bookentity.setprice (value); }Else if(Name.equals ("language") {bookentity.setlanguage (value); }} JDOMTest.bookList.add (bookentity); Bookentity=NULL;
Jar Package Import and export method with project:
Create a Lib folder, copy the jar package to this file
Build path Add Jar Package
2. DOM4J parsing xml file
To import a jar package:
Dom4j-1.6.1.jar
Packagecom.imooc_xml.dom4j.test;ImportJava.io.File;ImportJava.util.Iterator;Importjava.util.List;ImportOrg.dom4j.Attribute;Importorg.dom4j.Document;Importorg.dom4j.DocumentException;Importorg.dom4j.Element;ImportOrg.dom4j.io.SAXReader; Public classDom4jtest { Public Static voidMain (string[] args) {Saxreader reader=NewSaxreader (); Try{Document Document= Reader.read (NewFile ("Xml/books.xml")); Element Bookstore=document.getrootelement (); Iterator it=Bookstore.elementiterator (); while(It.hasnext ()) {Element Book=(Element) it.next (); List<Attribute> Bookattrs =book.attributes (); for(Attribute attr:bookattrs) {attr.getname (); Attr.getvalue (); } Iterator ITT=Book.elementiterator (); while(Itt.hasnext ()) {Element Bookchild=(Element) itt.next (); Bookchild.getname (); Bookchild.getstringvalue (); } } } Catch(documentexception e) {e.printstacktrace (); } }}
Analysis of four analytic methods:
Dom:
。 Platform-independent official parsing method
* Required memory performance is a bit higher
Sax:
。 An event-driven parsing approach provided by the Java Platform
* Analytic plots
Every step of the way triggers a different approach
JDOM, dom4j:
。 Extended on DOM, sax methods, only parsing methods that can be used in Java
*
Performance test:
Build Path-->add Library-->junit
@Test
public void Testperformance () throws exception{
Long start = system.currenttimemillis (); // Performance of test methods long end = system.currenttimemillis (); System.out.println (start-end);
Java--xml file Read (jdom&dom4j)