1 / 14

Using JDOM for XML processing

Using JDOM for XML processing. buildJDOM. I started with the sample code DescendantsDemo in samples but you could start with SAXBuilder also. Get attributes & child count. itr = doc.getDescendants(new ElementFilter()); while (itr.hasNext()) {

aderyn
Download Presentation

Using JDOM for XML processing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using JDOM for XML processing

  2. buildJDOM • I started with the sample code DescendantsDemo in samples but you could start with SAXBuilder also

  3. Get attributes & child count • itr = doc.getDescendants(new ElementFilter()); • while (itr.hasNext()) { • Element e= (Element) itr.next(); • if(e.getName().equals("person")){ • List AttList=e.getAttributes(); • Iterator attit=AttList.iterator(); • while (attit.hasNext()) { • attlen++; • Attribute a=(Attribute)attit.next(); • System.out.println("attribute:"+a.getValue() ); • } • List Children=e.getChildren(); • Iterator childit=Children.iterator(); • while (childit.hasNext()) { • childlen++; • Element el=(Element)childit.next(); • System.out.println("child:"+el.getValue() ); • } • }//if • }//while • attlen/=namecount; • childlen/=namecount;

  4. output • C:\PROGRA~1\java\JDK15~1.0_0\bin>java JDOMGetStructure sports.xml • attribute:Snowboarding • attribute:true • attribute:5 • child:Mary • child:IBM • attribute:Snowboarding • attribute:true • attribute:3 • child:Alison • child:DEC • attribute:Snowboarding • attribute:true • attribute:200 • child:Kathy • child:Apple • attribute:Speed reading • attribute:true • attribute:20 • child:Sharon • child:Mac • attribute:Snowboarding • attribute:true • attribute:100 • child:Philip • child:Milne • Number of names: 5 • Number of name attributes: 3 • Number of names children: 2 • C:\PROGRA~1\JAVA\JDK15~1.0_0\BIN>

  5. Contents of data[][] (space delimited) • Mary Campione Snowboarding true • Alison Huml Snowboarding true • Kathy Walrath Snowboarding true • Sharon Zakhour Speed reading true • Philip Milne Snowboarding true

  6. The code to build array data=new Object[namecount][attlen+childlen]; //fill the array int sub=-1; itr = doc.getDescendants(new ElementFilter()); while (itr.hasNext()) { len=0; Element e= (Element) itr.next(); if(e.getName().equals("person")){ sub++; List Children=e.getChildren(); Iterator childit=Children.iterator(); while (childit.hasNext()) { Element el=(Element)childit.next(); System.out.println(el.getValue()+" sub "+sub+" len "+len); data[sub][len++]=el.getValue() ; } List AttList=e.getAttributes(); Iterator attit=AttList.iterator(); while (attit.hasNext()) { Attribute a=(Attribute)attit.next(); data[sub][len++]=(String)a.getValue() ; } }//if }//while for(int row=0;row<namecount;row++){ for(int col=0;col<attlen+childlen-1;col++)System.out.print(data[row][col]+" "); System.out.println("

  7. JTable, JTree • Posted examples show how to build a JTable or JTree from an Object[][] array.

  8. SAXBuilder- pretty prints XML C:\PROGRA~1\JAVA\JDK15~1.0_0\BIN>java SAXEx sports.xml <?xml version="1.0" encoding="UTF-8"?> <root> <!--This is a simple XML file of people, sports, etc--> <person Sport="Snowboarding" Vegetarian="true" Years="5"> <FirstName>Mary</FirstName> <LastName>Campione</LastName> </person> <person Sport="Snowboarding" Vegetarian="true" Years="3"> <FirstName>Alison</FirstName> <LastName>Huml</LastName> </person> <person Sport="Snowboarding" Vegetarian="true" Years="200"> <FirstName>Kathy</FirstName> <LastName>Walrath</LastName> </person> <person Sport="Speed reading" Vegetarian="true" Years="20"> <FirstName>Sharon</FirstName> <LastName>Zakhour</LastName> </person> <person Sport="Snowboarding" Vegetarian="true" Years="100"> <FirstName>Philip</FirstName> <LastName>Milne</LastName> </person> <?myInstruction action silent?> </root>

  9. JTreeDemo: code in notes

  10. JTree • The add button makes this look attractive • It adds a child to the root

  11. Sports.xml from JDOMJTree

  12. Display is lacking but add button works

  13. After adding another person

  14. notes • Xml/doc was not altered. This would need to be done. • No dtd or schema was used. • Xml was not saved.

More Related