using jdom for xml processing l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Using JDOM for XML processing PowerPoint Presentation
Download Presentation
Using JDOM for XML processing

Loading in 2 Seconds...

play fullscreen
1 / 14

Using JDOM for XML processing - PowerPoint PPT Presentation


  • 102 Views
  • Uploaded on

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()) {

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Using JDOM for XML processing' - aderyn


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
buildjdom
buildJDOM
  • I started with the sample code DescendantsDemo in samples but you could start with SAXBuilder also
get attributes child count
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
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
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
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
JTable, JTree
  • Posted examples show how to build a JTable or JTree from an Object[][] array.
saxbuilder pretty prints xml
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
JTree
  • The add button makes this look attractive
  • It adds a child to the root
notes
notes
  • Xml/doc was not altered. This would need to be done.
  • No dtd or schema was used.
  • Xml was not saved.