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