GeeCON Prague 2014*
The moose likes XML and Related Technologies and the fly likes Checking the attribute value before extracting its data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Checking the attribute value before extracting its data" Watch "Checking the attribute value before extracting its data" New topic
Author

Checking the attribute value before extracting its data

H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
Hello

I hope (to start with) that this question is in the right place

I'm using JDOM library in order to create and manipulate a XML file..

To make things clear, here is the XML file...



Now what i want to do is to traverse the whole file and check whether element "c" is of type "1" or "2", so that i can deal with its data differently...

How do i do that?

i know how to access each element and its sub-elements, and i know how to traverse the file, all i need is to perform the check before accessing the values...

thanks :thumb:
HannaH
[ November 02, 2007: Message edited by: H Melua ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

So you know how to find an Element object in your JDOM tree, and the only thing you are missing is how to get an attribute from that Element? In that case you should just look in the JDOM documentation; go to the page for Element and see if you can find a method that gets an attribute.
H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
thanks, i know it and since i started this work i put it as my home page

but now i realised what i was doing wrong!! i was using the == to compare my strings!! and that doesnt work for strings so i fixed it

but my problem now is how to reach "c" of type 2
when i call element.getChild("c").getAttributeValue(), it only returns "1"!
[ November 03, 2007: Message edited by: H Melua ]
H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172

I've solved the second problem using XPath

HannaH
[ November 03, 2007: Message edited by: H Melua ]
 
GeeCON Prague 2014
 
subject: Checking the attribute value before extracting its data