Java Parsing XML file

Source: Internet
Author: User

Import the Jdom package.

One: The contents of the XML file 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> 

Importorg.jdom.Document;Importorg.jdom.Element;Importorg.jdom.JDOMException;ImportOrg.jdom.input.SAXBuilder; Public classTest { Public Static voidMain (string[] args)throwsexception{saxbuilder SB=NewSaxbuilder (); //InputStream in = Test.class.getClassLoader (). getResourceAsStream ("D:\\test.xml");File File =NewFile ("D:\\test.xml"); InputStream in=Newfileinputstream (file); Document Doc=Sb.build (in); //Document Doc=sb.build (Test.class.getClassLoader (). getResourceAsStream ("Test.xml"));//Constructing Document ObjectsElement root=doc.getrootelement ();//get root element HDList List=root.getchildren ("Disk");//take all the elements named disk         for(intI=0;i<list.size (); i++) {element element=(Element) list.get (i); String name= Element.getattributevalue ("name");//get the Name property valueString Capacity=element.getchildtext ("Capacity");//take the contents of the disk child element capacityString directories=element.getchildtext ("directories");//take the contents of the disk child element directoriesString Files=element.getchildtext ("files");//fetch the contents of the disk sub-element filesSYSTEM.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:

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
-----------------------------------

Two: Parse the incoming XML-formatted string:

Importorg.jdom.Document;Importorg.jdom.Element;Importorg.jdom.JDOMException;ImportOrg.jdom.input.SAXBuilder; Public classTest { Public Static voidMain (string[] args)throwsexception{saxbuilder SB=NewSaxbuilder (); //InputStream in = Test.class.getClassLoader (). getResourceAsStream ("D:\\test.xml");//File File = new file ("D:\\test.xml");//InputStream in = new FileInputStream (file);//Document Doc=sb.build (in);String str= "<?xml version=\" 1.0\ "encoding=\" utf-8\ "? >; InputStream in=NewBytearrayinputstream (Str.getbytes ()); InputStream in2=NewBytearrayinputstream (Str.getbytes ("UTF-8")); Document Doc=Sb.build (in2); //Document Doc=sb.build (Test.class.getClassLoader (). getResourceAsStream ("Test.xml"));//Constructing Document ObjectsElement root=doc.getrootelement ();//get root element HDList List=root.getchildren ("Disk");//take all the elements named disk         for(intI=0;i<list.size (); i++) {element element=(Element) list.get (i); String name= Element.getattributevalue ("name");//get the Name property valueString Capacity=element.getchildtext ("Capacity");//take the contents of the disk child element capacityString directories=element.getchildtext ("directories");//take the contents of the disk child element directoriesString Files=element.getchildtext ("files");//fetch the contents of the disk sub-element filesSYSTEM.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 ("-----------------------------------"); }                 }      }

Java Parsing 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.