HiddenLeafCoder
HiddenLeafCoder

Reputation: 131

How to get sibling tag values in XML using python ElementTree

I've been trying to get the value of the ci-name element whose sibling RandomAttribute's value(text) is IMPORTANT. I'm a beginner to python and I'm trying using Python's inbuilt ElementTree.

Here's the sample XML:

<state>
    <s-name>State 1</s-name>
    <district>
        <d-name>District 1</d-name>
        <city>
            <ci-name>City 1</ci-name>
            <RandomAttribute>UN- IMPORTANT</RandomAttribute>
        </city>
        <city>
            <ci-name>City 2</ci-name>
            <RandomAttribute>NOT SO IMPORTANT</RandomAttribute>
        </city>
        <city>
            <ci-name>City 3</ci-name>
            <RandomAttribute>IMPORTANT</RandomAttribute>
        </city>
    </district>
</state>

Please help me out with this.

Upvotes: 2

Views: 1383

Answers (3)

Shrikant Gourh
Shrikant Gourh

Reputation: 88

import lxml.etree as etree

if xml you are taking about is string then

xml_xpath = "/state/district/city[RandomAttribute='IMPORTANT']/ci-name/text()"
XML_dom = etree.XML(xml)
XML_XPATH_name = etree.XPath(xml_xpath)
return XML_XPATH_name(XML_dom)

Upvotes: 0

GaiusJulius
GaiusJulius

Reputation: 83

Not a solution in one line like the previous one, but I like to keep track of how the XML is structured.

Maybe this helps you understand how ElementTree works?

import xml.etree.ElementTree as ET

xml = 'temp.xml'
xmltree = ET.parse(xml)
cities = xmltree.findall('district')[0].findall('city')
for city in cities:
    RandAttribute = city.findall('RandomAttribute')[0].text
    if RandAttribute == "IMPORTANT":
        ci_name = city.findall('ci-name')[0].text
        print(ci_name)

Upvotes: 2

zx485
zx485

Reputation: 29052

You can access the value with the XPath-1.0 expression

/state/district/city[RandomAttribute='IMPORTANT']/ci-name

Just put this into a Python XML processor.
In lxml this can look like

xml = ...
tree = etree.parse(xml)
result = tree.xpath('/state/district/city[RandomAttribute='IMPORTANT']/ci-name')
print(result[0].tag)

Output should be

City 3

Upvotes: 4

Related Questions