bozogs
bozogs

Reputation: 19

python ElementTree - find value of child's attribute based on parent's attribute

Here is a example of what's in my XML file.

<parent    uniqueID="1000" name="Bob Ross"   >
    <child     atrribute1="value"
</parent>

The user is going to search for a uniqueID and the program will return various bits of information.

Using element tree I get return the value of the 'name' in parent using this

x = root.find("./parent[@uniqueID='"+entered_text+"']").attrib['name']

I would like to get the child's element 'attribute1' based on the uniqueID the user has search for. I am just not sure how to do it.

I have been searching and looking through the documentation, however I am quite new to python and element tree so I may have missed it.

Many thanks, bozogs

Upvotes: 0

Views: 1024

Answers (1)

bozogs
bozogs

Reputation: 19

Typical I work it out after posting on here - here is the answer for anyone who stumbles across this question

Using the Xpath you just add another / on as it's already looking at the parent.

root.find(".//parent[@uniqueID='" + entered_text + "']/child").attrib['attribute1']

Upvotes: 2

Related Questions