Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pulling xml attributes with java

Status
Not open for further replies.

jimmybambino

Programmer
Jun 4, 2003
5
US
Can someone tell me how I would pull an attribute value from a xml element if I had a value of another attribute within that same element? I need to do this with java. Can you help?

Thanks
 
>> if I had a value of another attribute

huh? no idea what that means [bugeyed]

-pete
 
I'm sorry, my question wasn't worded in the most clear manner. For example, if I had this:

<Root>
<Element attr1 = &quot;value1&quot; attr2 = &quot;value2&quot;/>
<Element attr1 = &quot;anothervalue1&quot; attr2 = &quot;anothervalue2&quot;/>
</Root>

and I wanted to search for the element that had the attr2 attribute value of &quot;value2&quot; and need to return the value of the attr1 attribute. Can you do this in Java? I'm not sure if this is more clear. I hope so.
 
If you mean return the String from attr1 after finding the Element using attr2 as a filter, yes.

Are you parsing the XML into a DOM tree? If so you would perform an XPath query using //Element[@attr2=’value2’]

Then cast the returned Node to an Element and return it’s getAttribute(“attr1”);


-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top