Home > Software design >  How to get all children values out of a XML file? [python]
How to get all children values out of a XML file? [python]

Time:12-07

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]

CodePudding user response:

public class ExtractElement {

public interface Projection {

    interface SelectedElement {
        @XBRead("name()")
        String getName();
        @XBRead(".")
        String getValue();
    }

    @XBRead("//{0}/*")
    List<SelectedElement> getSelectedElements(String name);
}
public static void main(String[] args) throws IOException {
    Projection projection = new XBProjector().io().url("resource://data.xml").read(Projection.class);        
    for (SelectedElement se:projection.getSelectedElements("selectedElement")) {
        System.out.println("Found element with name " se.getName() " and value " se.getValue());
    }    
}    

}

CodePudding user response:

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)  

CodePudding user response:

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']
  • Related