[jdom-interest] newbie question

Sylvain.Thevoz at swisscom.com Sylvain.Thevoz at swisscom.com
Wed Dec 4 06:20:43 PST 2002


Thank you all!!

My goal is to use JDOM to load an XML file, read it and retrieve content. This content will be used as a ini file in a XForm application.
So my examples was just to understand the JDOM concepts.

Maybe a bit more difficult:
I have a XML file like this
<document>
  <group name="Europe">
    <company name="LH">
      <fullname>Lufthansa</fullname>
    </company>
    <company name="SA">
      <fullname>Soviet Airlines</fullname>
    </company>
    <company name="AF">
      <fullname>Air France</fullname>
    </company>
  </group>
  <group name="US">
    <company name="AA">
      <fullname>American Airlines</fullname>
    </company>
  </group>
</document>


I want to retrieve content of this file and create a list of String that contain:
Europe
Europe->Lufthansa
Europe->Soviet Airlines
Europe->Air France
US
US->American Airlines


Anyone has experience?
Thank you very much
Sylvain



-----Message d'origine-----
De: Kenneth Ellefsen [mailto:kellefsen at bergen.oilfield.slb.com]
Date: mercredi, 4. décembre 2002 14:29
À: Thévoz Sylvain, IT-DCS-CPS-CLI-DAR
Objet: Re: [jdom-interest] newbie question


hi.

Your document does only contain one element at the "root" level
"<document>", so you have to get the children of that element and list
them...

To just paste the whole document to System.out an xml parser seems a bit
over kill :) But I guess that's not what your trying to do even though
that's your example.


try:


Element items = 
document.getRootElement().getChild("typeOfWorkstation").getChildren();

ListIterator itemIterator = items.listIterator();

while(itemIterator.hasNext()){

   System.out.println(
      ((Element)itemIterator.next()).getChildText("description")
   );
}


I didn't try this...so there might be some errors :))

Regards....


On Wed, 2002-12-04 at 14:04, Sylvain.Thevoz at swisscom.com wrote:
> Hello,
> 
> I'm trying to use JDOM instead of DOM because I think it's a very interesting work to make XML stuffs easier.
> 
> But there is something that I don't understand (or maybe I have a wrong code):
> 
> I have this XML file:
> <document>
>   <typeOfWorkstation>
>     <item id="desktop">
>       <description>Desktop</description>
>     </item>
>     <item id="laptop">
>       <description>Laptop</description>
>     </item>
>     <item id="other">
>       <description>Other configuration</description>
>     </item>
>   </typeOfWorkstation>
> </document>
> 
> Now I want to load this file in a JDOM document:
> SAXBuilder builder = new SAXBuilder();
> Document document = builder.build(new File("files\\myfile.xml"));
> 
> OK!
> 
> 1) Then I want to get the content of this document. I use the getContent() method:
> List docContent = document.getContent();
> ListIterator iterator = docContent.listIterator();
> while (iterator.hasNext()) {
>   Element elements = (Element) iterator.next();
>   System.out.println(elements);
> }
>  => it returns only: [Element: <document/>]
> 
> Am I in the right way??
> I would like to see all XML tree.
> 
> 
> How do you do if you want to display all contents of the <description> tags?
> 
> Thank you
> Sylvain
> 
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com
-- 
Mvh,

Kenneth Ellefsen                   | Tlf: (+47) 55985829
Schlumberger Information Solutions | www.sis.slb.com





More information about the jdom-interest mailing list