0

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

1 Answer 1

2

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']
Sign up to request clarification or add additional context in comments.

1 Comment

I was about to say that! Good that you figured it out. You can also just get the parent-element with x = root.find(".//parent[@uniqueID='" + entered_text + "']]"). Then the child-elements can be found by indexing the parent, so the child-element would be child = x[0], and you can do what you already did: attribute1 = child.attrib['attribute1'].

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.