140 likes | 300 Views
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()) {
E N D
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()) { • 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;
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>
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
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("
JTable, JTree • Posted examples show how to build a JTable or JTree from an Object[][] array.
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>
JTree • The add button makes this look attractive • It adds a child to the root
notes • Xml/doc was not altered. This would need to be done. • No dtd or schema was used. • Xml was not saved.