There are many ways to parse XML files in Java, and it is most commonly used to parse XML files using DOM4J. The following is a brief introduction to the basic use of dom4j.
DOM4J need jar package support, you can download from the network, such as Dom4j-1.6.1.jar;
Download good jar package, as long as the import of our project can be used, Java WEB Project Import path is: Webroot/web-inf/lib directory;
code example:
First, view the XML document underlying operations (shown in the encapsulation method):
Public Static voidRun ()throwsException {//Get dom4j Parser ObjectSaxreader reader =NewSaxreader (); //reads XML, returns the Document objectDocument document = Reader.read ("Src/demo.xml"); //Get root nodeElement rootelement =document.getrootelement (); //gets the element collection for all the elements under the root node named booklist<element> elements = rootelement.elements ("book"); //Select an elementElement shu2 = elements.get (1); //get a target element under an elementElement name2 = shu2.element ("name"); //gets the text content of the element GetText ()System.out.print ("title is:" +Name2.gettext ()); }
To be continued .......
Dom4j of Java Parsing xml