Home » excel » How to get data of a specific root in XML(by python)

How to get data of a specific root in XML(by python)

Posted by: admin May 14, 2020 Leave a comment

Questions:

I want to convert xml file to excel by xml.etree.ElementTree.

And I want to read data from a specific root.

Assume my xml looks like

<?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>

If I use ‘iter’ directly, I will get :

for neighbor in root.iter('neighbor'):
  print neighbor.attrib
{'name': 'Austria', 'direction': 'E'}
{'name': 'Switzerland', 'direction': 'W'}
{'name': 'Malaysia', 'direction': 'N'}
{'name': 'Costa Rica', 'direction': 'W'}
{'name': 'Colombia', 'direction': 'E'}

But I only want to get all the neighbor of ‘ Liechtenstein ‘
That means I want my script give me

{'name': 'Austria', 'direction': 'E'}
{'name': 'Switzerland', 'direction': 'W'}

only.

Which function should I use?

How to&Answers:

you can do:

x = """<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>
"""

import xml.etree.ElementTree as ET
data = ET.fromstring(x) //here x is xml string
for child in data:
    if child.attrib['name'] == 'Liechtenstein':
        for grandchild in child:
            if grandchild.tag == 'neighbor':
                print grandchild.attrib

Answer:

Installation

pip3 install lxml

Code

from lxml import etree

with open('countries.xml', 'r') as f:
    root = etree.fromstring(f.read())

neighbors = root.xpath('/data/country[@name="Liechtenstein"]/neighbor')

for n in neighbors:
    n_names = n.xpath('@name')
    n_name = n_names[0]
    n_directions = n.xpath('@direction')
    n_direction = n_directions[0]
    print(n_name, n_direction)

Output

Austria E
Switzerland W

Tested on Python 3.6.0.

If you use Python 2.7: Replace pip3 with pip and print(n_name, n_direction) with print n_name, n_direction. Have fun.