-1

I'm working with an API that outputs all its XML with the same <FL> key and uses the same attribute name:

<response uri="/webaddress/">
  <result>
    <Quotes>
      <row no="1">
        <FL val="ID">12345</FL>
        <FL val="Number">
          <![CDATA[ 12346 ]]>
        </FL>
        <FL val="Subject">
          <![CDATA[ Test ]]>
        </FL>
        <FL val="Stage">
          <![CDATA[ Draft ]]>
        </FL>
        <FL val="Valid Till">
          <![CDATA[ 2016-01-23 ]]>
        </FL>
        <FL val="CONTACTID">12121</FL>

I'm sure I can still work with it, but I'm pretty new and I'm struggling to find any examples that apply. I'm working in Python 3.5 with xml.etree but I don't really care what library I'm working with.

Could someone offer a example of how to get the content out of an element given a particular name-value attribute pair, i.e. store the '12345' or '12346' as a variable?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Ben J.
  • 766
  • 1
  • 6
  • 17

1 Answers1

0

The documentation gives some examples along with explanation. For example:

import xml.etree.ElementTree as ET
tree = ET.parse('data.xml')
root = tree.getroot()

for fl in root.iter('FL'):
    val = fl.attrib['val']
    text = fl.text.strip()
    print("The %s is %s" % (val, text))
dsh
  • 12,037
  • 3
  • 33
  • 51