plxhelpme_kindofuser
plxhelpme_kindofuser

Reputation: 113

Python 2.7 xml.etree. Getting sibling

I got xml with a structure similar to following example

            [...]
            <a>
            <list>
            <section>
                <identifier root="88844433"></templateId>
                <code code="6664.2" display="Relevant"></code>
                <title>Section title </title>
            </section>
            </a>
            </list>
            [...]

How can I get title block searching it by root attribute of identifier block using xml.etree in Python2.7?

Upvotes: 0

Views: 43

Answers (1)

balderman
balderman

Reputation: 23815

below

import xml.etree.ElementTree as ET

xml = '''   <a>
            <list>
            <section>
                <templateId root="12"></templateId>
                <code code="6664.2" display="Relevant"></code>
                <title>Section title </title>
            </section>
            </list>
            </a>'''

root = ET.fromstring(xml)
section = root.find(".//section/templateId[@root='12']/..")
print(section.find('title').text)

output

Section title

Upvotes: 1

Related Questions