-1

Example:

<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>The house is on the hill</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>
</data>

I would like to get all values inside "country" without considering the child nodes, like this:

[1, 2008, 'The house is on the hill', 4, 2011, 59900]

2 Answers2

0

something like this:

import xml.etree.ElementTree as ET

# Parse the XML file
tree = ET.fromstring('''<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>
</data>''')

# Create an empty list to store the child values
child_values = []

# Iterate through the country elements
for country in tree.findall('country'):
  # Extract the text value of each child element
  for child in country:
    if child.text:
      child_values.append(child.text)

# Print the list of child values
print(child_values)  
mucio
  • 7,014
  • 1
  • 21
  • 33
0

The below seems to work (note that all values are strings)

import xml.etree.ElementTree as ET

xml = '''<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>The house is on the hill</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>
</data>'''

data = []
root = ET.fromstring(xml)
for country in root.findall('.//country'):
    for sub in ['rank', 'year', 'gdppc']:
        data.append(country.find(sub).text)
print(data)

output

['1', '2008', 'The house is on the hill', '4', '2011', '59900']
balderman
  • 22,927
  • 7
  • 34
  • 52