Parse XML using JDOM + xpath

Source: Internet
Author: User

Parse XML with JDOM

All classes of the JDOM model are in org. JDOM. * In this package, org. JDOM. input. * This package contains the JDOM parser. The dombuilder function is to parse the document of the DOM model into the document of the JDOM model; the function of saxbuilder is to parse the XML tree conforming to the JDOM model from a file or stream. Since the XML sample we mentioned above is stored in a file named sample. XML, it is clear that we should use the latter as a parsing tool. The following program demonstrates the basic functions of JDOM, namely parsing an XML document and selecting 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) 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:" + name); system. out. println ("partition capacity:" + capacity); system. out. println ("number of directories:" + Directories); system. out. println ("number of files:" + files); system. out. println ("-----------------------------------");}}}

Program output result:

Disk information: partition drive: C partition capacity: 8 GB directory count: 200 file count: 1580 ------------------------------------- disk information: partition drive letter: D partition capacity: 10 Gb directory count: 500 file count: 3000 bytes -----------------------------------

This program uses the traditional parsing method. The first-level data is collected from the root node to the subnode one by one, which is quite satisfactory. Imagine if this tree is deep enough and we want to fetch data from the third node in layer 0 (exaggerated), it would be a nightmare! The following content will ease your pain.



Back to Top

JDOM + XPath advanced

With so many benefits of JDOM and XPath, it is time for a hero to be useful.

The XPath API of JDOM is in the org. JDOM. XPath package. Look at this package. There is only one class. JDOM is so concise that nothing is as complicated as it is. The core APIs in this class are selectnodes () and selectsinglenode (). The former returns a group of nodes based on an XPATH statement, and the latter returns the first node that meets the criteria based on an XPATH statement.

The following program uses JDOM + XPath to implement the same functions of the previous program. You can learn a lot about using XPath:

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 ()")). gettextnormalize (); 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 ()")). gettextnormalize (); system. out. println ("disk information:"); system. out. println ("partition drive:" + 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: 8 GB directory count: 200 file count: 1580 ------------------------------------- disk information: partition drive letter: D partition capacity: 10 Gb directory count: 500 file count: 3000 bytes -----------------------------------

Category: J2EE | add to souzang
| Share to I post it | browse (243) | comment
(1)

Previous Article: Integrating flex into Java EE application... next article: foreground ry of WAPI Recent readers:
After logging on, you will be here.
Related Article

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.