Jaycel Cunanan
Jaycel Cunanan

Reputation: 33

Get Child node in XML python

I'm a newbie on python and XML and currently I'm using python 3.6 and I have this XML data in a variable

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>

What I want to do is get the "child node"? and place it to a variable like this one

var1 = '<country name="Liechtenstein">
    <rank>1</rank>
    <year>2008</year>
    <gdppc>141100</gdppc>
    <neighbor name="Austria" direction="E"/>
    <neighbor name="Switzerland" direction="W"/>
</country>'

var2 = '<country name="Singapore">
    <rank>4</rank>
    <year>2011</year>
    <gdppc>59900</gdppc>
    <neighbor name="Malaysia" direction="N"/>
</country>'

...etc

is there any way to get the result

Upvotes: 1

Views: 15095

Answers (2)

toheedNiaz
toheedNiaz

Reputation: 1445

Here is my approach to solve this :

you can make a list of all the country elements and then do some operations on that list as @holdenweb has mentioned the country nodes might be variable in each xml you have so , I am using a list to keep the nodes inside that list. you can iterate that list depending upon you requirement.

Code :

import xml.etree.ElementTree as ET

xml = """<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>"""

nodeList = []
root = ET.fromstring(xml)
for nod in root.findall("country"):
    nodeList.append(ET.tostring(nod))
    print(str(ET.tostring(nod) + b"\n"))

Output :

b'<country name="Liechtenstein">\n        <rank>1</rank>\n        <year>2008</year>\n        <gdppc>141100</gdppc>\n        <neighbor direction="E" name="Austria" />\n        <neighbor direction="W" name="Switzerland" />\n    </country>\n    \n'
b'<country name="Singapore">\n        <rank>4</rank>\n        <year>2011</year>\n        <gdppc>59900</gdppc>\n        <neighbor direction="N" name="Malaysia" />\n    </country>\n    \n'
b'<country name="Panama">\n        <rank>68</rank>\n        <year>2011</year>\n        <gdppc>13600</gdppc>\n        <neighbor direction="W" name="Costa Rica" />\n        <neighbor direction="E" name="Colombia" />\n    </country>\n\n'

Upvotes: 1

holdenweb
holdenweb

Reputation: 37153

You probably don't want to assign each country data to its own variable, unless you are absolutely sure that the number of them is small and does not vary. Therefore it would be simplest to use looping to handle each country:

data = ...
from xml.etree import ElementTree
tree = ElementTree.fromstring(data)
countries = tree.findall("country")

You now have a list of countries that you can iterate over to analyse each one further.

for country in countries:
    print(country)

<Element 'country' at 0x10e51d818>
<Element 'country' at 0x10e535688>
<Element 'country' at 0x10e535cc8>

Upvotes: 0

Related Questions