[jdom-interest] How to parse this xml?

Kian On, Yong kian.on.yong at alfacomtech.com
Sun Nov 5 20:55:39 PST 2000


Hi,

I have the following xml file...

***************************************************
<?xml version="1.0"?>

<computer>
    <type>Personal Computer</type>
        <subtype>Dell</subtype>
        <subtype>Acer</subtype>
        <subtype>Samsung</subtype>
    <type>MiniComputer</type>
        <subtype>MC1</subtype>
        <subtype>MC2</subtype>
        <subtype>MC3</subtype>
</computer>
***************************************************

I use the following code...to parse the data out
from this xml but not really getting the correct
result.

***************************************************
// Build the JDOM Document
Document JDOMDoc = saxbuilder.build(new File(filename));

// Get the root element
Element root = JDOMDoc.getRootElement();

/* Print computer type information*/
List types = root.getChildren("type");
out.println("Types of computer has " + types.size() + ".");
Iterator i = types.iterator();
while (i.hasNext()) {
   Element type = (Element) i.next();
   out.println( "   * <" + type.getText() + ">" );

   /* Print computer subtype information*/
   List subtypes = type.getChildren("subtype");
   out.println("\t--Subtypes of this " + type.getText() +
                           " has " + subtypes.size() + ".");
   Iterator j = subtypes.iterator();
   while ( j.hasNext() ) {
      Element subtype = (Element) j.next();
      out.println( "\t\t" + subtype.getText() );
   }
}
***************************************************

I got no problem to get "<type>" element but 
could not get "<subtype>" data.

subtypes.size() return 0 to me!
I don't know why?

Anyone can guide me on this matter? Thanks!!!

Kian On, Yong


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20001106/268eef29/attachment.htm


More information about the jdom-interest mailing list