Excerpt from the JAVA JDOM operation XML file

Source: Internet
Author: User
Tags xpath

Jdom cultivation of the article

Programmers who have used xerces will feel that sometimes a word can be said clearly, when using the Xerces API to achieve, to three or four lines of the program.

Back to top of page

Get and install Jdom

At http://www.jdom.org/you can download the latest version of Jdom and add all the jar packages in the Jdom.jar and LIB directories in the package to classpath.

Back to top of page

Parsing XML with Jdom

All classes of the Jdom model are in org.jdom.* this package, org.jdom.input.* This package contains the Jdom parser, wherein the Dombuilder function is to parse the DOM model document into the JDOM model document The function of Saxbuilder is to parse an XML tree that conforms to the Jdom model from a file or stream. Since the XML sample we mentioned above is stored in a file called Sample.xml, it is clear that we should use the latter as the parsing tool. The following program demonstrates the basic function of jdom, which is to parse an XML document and pick some content to output to the screen.

Import Java.util.*;import Org.jdom.*;import Org.jdom.input.saxbuilder;public class Sample1 {public static void main ( String[] args) throws exception{   Saxbuilder sb=new saxbuilder ();  Document doc=sb.build ("Sample.xml");  Element root=doc.getrootelement ();  List List=root.getchildren ("Disk");  for (int i=0;i<list.size (); i++) {   element element= (Element) list.get (i);   String name=element.getattributevalue ("name");   String Capacity=element.getchildtext ("Capacity");   String directories=element.getchildtext ("directories");   String Files=element.getchildtext ("files");   SYSTEM.OUT.PRINTLN ("Disk information:");   SYSTEM.OUT.PRINTLN ("Partition Drive letter:" +name);   SYSTEM.OUT.PRINTLN ("Partition Capacity:" +capacity);   System.out.println ("Number of directories:" +directories);   System.out.println ("Number of files:" +files);   System.out.println ("-----------------------------------");}}   

The output of the program:

Disk information: partition drive: C partition Capacity: 8G Number of directories: 200 files: 1580-----------------------------------disk information: partition drive: D partition capacity: 10G Number of files: 500 file:-------- ---------------------------

This procedure uses the traditional analytic method, the first level from the root node to the child node one by one collects the data which we need, is very. Imagine if this tree is deep enough, we want to take the third node of the 5th 0 layer data (exaggerated point, hehe), it will be a nightmare! The following content will easily dissolve your pain.

Jdom+xpath Advanced Article

Having said so many jdom and XPath benefits, it's time for heroes to come into play.

Jdom's XPath API is org.jdom.xpath in this package. Look at this package, there is only one class, Jdom is so concise, nothing is so complicated. The core APIs in this class are mainly two selectnodes () and selectSingleNode (). The former returns a set of nodes based on an XPath statement, which returns the first node that meets the criteria based on an XPath statement.

The following program we use Jdom+xpath to achieve the same function of the previous program, you can learn a lot of the use of XPath knowledge:

Import Java.util.*;import org.jdom.*;import Org.jdom.input.saxbuilder;import Org.jdom.xpath.xpath;public class  Sample2 {public static void main (string[] args) throws Exception {Saxbuilder sb = new Saxbuilder ();  Document doc = Sb.build ("Sample.xml");  Element root = Doc.getrootelement ();  List List = Xpath.selectnodes (Root, "/hd/disk");   for (int i = 0; i < list.size (); i++) {Element disk_element = (Element) list.get (i);   String name = Disk_element.getattributevalue ("name"); String capacity = ((Text) Xpath.selectsinglenode (disk_element, "//disk[@name = '" + name + "']/capacity/text ()")). Gette   Xtnormalize (); String directories = ((Text) Xpath.selectsinglenode (disk_element, "//disk[@name = '" + name + "']/directories/text ()")   ). Gettextnormalize (); String files = ((Text) Xpath.selectsinglenode (disk_element, "//disk[@name = '" + name + "']/files/text ()")). Gettextnor   Malize ();   SYSTEM.OUT.PRINTLN ("Disk information:");   SYSTEM.OUT.PRINTLN ("Partition drive letter:" + name); System.out.printlN ("Partition capacity:" + capacity);   System.out.println ("Number of directories:" + directories);   System.out.println ("Number of files:" + files);  System.out.println ("-----------------------------------"); } }}

Output Result:

Disk information: partition drive: C partition Capacity: 8G Number of directories: 200 files: 1580-----------------------------------disk information: partition drive: D partition capacity: 10G Number of files: 500 file:-------- ---------------------------

Excerpt from the JAVA JDOM operation XML file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.