Home > Software engineering >  Parsing a XML in Python using ElementTree with repeat elements
Parsing a XML in Python using ElementTree with repeat elements

Time:08-24

I have an XML file that is structured like

<?xml version="1.0" encoding="utf-8"?>
<Method>
  <Version>1.0</Version>
  <Parameter key="IM-MS Browser Method">
   <Parameter key="FileOperations">
      <Parameter key="CondenseFilePSet">
        <Parameter key="AutoFrameSelect">
          <Value>True</Value>
        </Parameter>
    </Parameter>
    <Parameter key="MethodBookmark">
      <Parameter key="MassRange">
        <Value />
      </Parameter>
      <Parameter key="PolygonRegion">
        <Value>
          <XUnits>Milliseconds</XUnits>
          <YUnits>MassToCharge</YUnits>
          <Vertices>
            <V x="0.2705684" y="102.2292" />
            <V x="59.79562" y="3173.849" />
            <V x="0.676421" y="3173.849" />
            <V x="0.2705684" y="142.8233" />
          </Vertices>
        </Value>
      </Parameter>

I would like to retrieve the values of "Vertices". How can I do this with python's native XML parser ElementTree? Thanks!

CodePudding user response:

Use xml.etree.ElementTree.Element.findall with an XPath expression that selects all Vertices tags on all levels beneath the root.

See https://docs.python.org/3/library/xml.etree.elementtree.html#supported-xpath-syntax

import xml.etree.ElementTree as ET

xml_string = r"""<?xml version="1.0" encoding="utf-8"?>
<Method>
  <Version>1.0</Version>
  <Parameter key="IM-MS Browser Method">
    <Parameter key="FileOperations">
      <Parameter key="CondenseFilePSet">
        <Parameter key="AutoFrameSelect">
          <Value>True</Value>
        </Parameter>
      </Parameter>
    </Parameter>
    <Parameter key="MethodBookmark">
      <Parameter key="PolygonRegion">
        <Parameter key="MassRange">
          <Value/>
        </Parameter>
        <Value>
          <XUnits>Milliseconds</XUnits>
          <YUnits>MassToCharge</YUnits>
          <Vertices>
            <V x="0.2705684" y="102.2292"/>
            <V x="59.79562" y="3173.849"/>
            <V x="0.676421" y="3173.849"/>
            <V x="0.2705684" y="142.8233"/>
          </Vertices>
        </Value>
      </Parameter>
    </Parameter>
  </Parameter>
</Method>"""

root = ET.fromstring(xml_string)

# It returns a list of Elements
vertices_tags = root.findall(".//Vertices")

for v_tags in vertices_tags:
    for v_tag in v_tags:
        print(v_tag.tag, v_tag.attrib)

Output:


V {'x': '0.2705684', 'y': '102.2292'}
V {'x': '59.79562', 'y': '3173.849'}
V {'x': '0.676421', 'y': '3173.849'}
V {'x': '0.2705684', 'y': '142.8233'}
  • Related