XML parsing Tool-jdom

Source: Internet
Author: User

Introduction : The recent contact with the SSH framework, often have to configure the XML file; today is nothing to dig up the principle of XML parsing for everyone to share. This article mainly resolves an XML file by a simple example. After knowing why, it's not difficult for people to define their own XML.

Jdom is an open source project that uses pure Java technology to parse, generate, serialize, and manipulate XML documents based on the tree structure. The parsing steps are simple enough to make an XML file analogy to a tree. First from the root node to parse, and then traverse the left and right child node, child node and then recursively parse the child node ... (Note that the traversal should be distinguished from the depth-first traversal, and the exact understanding should be hierarchical traversal!) )

• Tree-level parsing
· Java code

2 files (Test.xml and Sample1.java), please also download the Jdom.jar package yourself online.

Sample1.java

1 ImportJava.util.*;2 Importorg.jdom.*;3 ImportOrg.jdom.input.SAXBuilder;4 5 6  Public classSample1 {7      Public Static voidMain (string[] args)throwsException {8Saxbuilder SB =NewSaxbuilder ();9 TenDocument doc = Sb.build (Sample1.class. getClassLoader () One. getResourceAsStream ("Test.xml"));//Constructing Document Objects AElement root = Doc.getrootelement ();//get root element HD -List List = Root.getchildren ("Disk");//take all the elements named disk -          for(inti = 0; I < list.size (); i++) { theElement element =(Element) list.get (i); -  -String Capacity=element.getchildtext ("Capacity");//take the contents of the disk child element capacity -String directories=element.getchildtext ("directories"); +String Files=element.getchildtext ("Files");  -               +SYSTEM.OUT.PRINTLN ("Disk Information:"); ASYSTEM.OUT.PRINTLN ("Partition Capacity:" +capacity); atSystem.out.println ("Number of directories:" +directories); -System.out.println ("Number of files:" +files); -System.out.println ("-----------------------------------");} -     } -}

Test.xml

<?XML version= "1.0" encoding= "UTF-8"?><HD>  <Diskname= "C">    <capacity>8G</capacity>    <Directories>200</Directories>    <Files>1580</Files>  </Disk>  <Diskname= "D">    <capacity>10G</capacity>    <Directories>500</Directories>    <Files>3000</Files>  </Disk></HD> 

• Analytic results
Disk information: Partition capacity: 8G Directory number: 200 files: 1580-----------------------------------disk Information: partition capacity: 10G Number of files: 500 file:-------------------- ---------------

 Conclusion : It is said that DOM4J analytic toolkit better use, have the opportunity to try ~ But its principle must be similar!

XML parsing Tool-jdom

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.