Jdom Learning to read XML files

Source: Internet
Author: User

To read an XML file with Jdom, you first create the document object with the build () method of the Org.jdom.input.SAXBuilder object, and then read the required content using the document class, element class, and so on. Ibm:developerworks China Station has a good example:

Java code
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <HD>
  3. <disk name="C" >
  4. <capacity>8G</capacity>
  5. <directories>200</directories>
  6. <files>1580</files>
  7. </disk>
  8. <disk name="D" >
  9. <capacity>10G</capacity>
  10. <directories>500</directories>
  11. <files>3000</files>
  12. </disk>
  13. </HD>

The above Sample.xml document describes the basic information of a hard disk in a computer (root node <HD> represent hard disk,<disk> tag represents hard disk partition, from its Name property can be seen two drive letter name "C" and "D" Partition Each partition contains <capacity>,<directories><files> three nodes, representing the partition's space size, number of directories, and number of files included.

The following program reads the information in this file:

Java code
  1. Import java.util.*;
  2. Import org.jdom.*;
  3. Import Org.jdom.input.SAXBuilder;
  4. Public class Sample1 {
  5. public static void Main (string[] args) throws exception{
  6. Saxbuilder sb=New Saxbuilder ();
  7. Document doc=sb.build ("sample.xml"); //Construct Document Object
  8. Element root=doc.getrootelement (); //Get root element
  9. List List=root.getchildren ("Disk"); Take all the elements named disk
  10. For (int i=0;i<list.size (); i++) {
  11. Element element= (Element) list.get (i);
  12. String Name=element.getattributevalue ("name");
  13. String Capacity=element.getchildtext ("Capacity"); Take the contents of the disk child element capacity
  14. String Directories=element.getchildtext ("directories");
  15. String files=element.getchildtext ("files");
  16. SYSTEM.OUT.PRINTLN ("Disk information:");
  17. SYSTEM.OUT.PRINTLN ("Partition drive letter:" +name);
  18. SYSTEM.OUT.PRINTLN ("Partition capacity:" +capacity);
  19. System.out.println ("Number of directories:" +directories);
  20. System.out.println ("Number of files:" +files);
  21. System.out.println ("-----------------------------------");
  22. }
  23. }
  24. }

Operation Result:
C:\java>java Sample1
Disk Information:
Partition Drive letter: C
Partition capacity: 8G
Number of catalogs: 200
Number of files: 1580
-----------------------------------
Disk Information:
Partition Drive letter: D
Partition capacity: 10G
Number of catalogs: 500
Number of files: 3000

Jdom Learning to read XML files

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.