e-pirate
e-pirate

Reputation: 193

XML attribute parsing with python and ElementTree

folks! I'm trying to parse some weird formed XML:

<?xml version="1.0" encoding="UTF-8"?>
<analytics>
  <standard1>
    ...
    <attributes>
      <attribute name="agentname" value="userx userx" />
      <attribute name="agentpk" value="5" />
      <attribute name="analytics:callid" value="757004000003597" />
      ...
      <attribute name="wrapuptime" value="0" />
    </attributes>
  </standard1>
  <standard2>
    ...
    <attributes>
      <attribute name="agentname" value="userx userx" />
      <attribute name="agentpk" value="5" />
      <attribute name="analytics:callid" value="757004000003597" />
      ...
      <attribute name="wrapuptime" value="0" />
    </attributes>
  </standard2>
  <engines>
  ...
  </engines>
</analytics>

Since both name and value are attributes, I have no idea how to access value by name without looping through the whole attributes subsection with a foreach cycle.

Any ideas how to get a direct access using ElementTree?

Upvotes: 0

Views: 1142

Answers (1)

alecxe
alecxe

Reputation: 474191

You can use a simple XPath expression to filter attribute element by the value of name attribute. Sample working code:

import xml.etree.ElementTree as ET

data = """<?xml version="1.0" encoding="UTF-8"?>
<analytics>
  <standard>
      <attributes>
        <attribute name="agentname" value="userx userx" />
        <attribute name="agentpk" value="5" />
        <attribute name="analytics:callid" value="757004000003597" />
        <attribute name="wrapuptime" value="0" />
      </attributes>
  </standard>
</analytics>
"""

root = ET.fromstring(data)
print(root.find(".//attribute[@name='agentname']").attrib["value"])

Prints:

userx userx

Beware that xml.etree.ElementTree has a limited XPath support.

Upvotes: 1

Related Questions