Roy Holzem
Roy Holzem

Reputation: 870

iterate through XML?

What is the easiest way to navigate through XML with python?

<html>
 <body>
  <soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:body>
    <getservicebyidresponse xmlns="http://www.something.com/soa/2.0/SRIManagement">
     <code xmlns="">
      0
     </code>
     <client xmlns="">
      <action xsi:nil="true">
      </action>
      <actionmode xsi:nil="true">
      </actionmode>
      <clientid>
       405965216
      </clientid>
      <firstname xsi:nil="true">
      </firstname>
      <id xsi:nil="true">
      </id>
      <lastname>
       Last Name
      </lastname>
      <role xsi:nil="true">
      </role>
      <state xsi:nil="true">
      </state>
     </client>
    </getservicebyidresponse>
   </soapenv:body>
  </soapenv:envelope>
 </body>
</html>

I would go with regex and try to get the values of the lines I need but is there a pythonic way? something like xml[0][1] etc?

Upvotes: 3

Views: 9627

Answers (1)

shad0w_wa1k3r
shad0w_wa1k3r

Reputation: 13372

As @deceze already pointed out, you can use xml.etree.ElementTree here.

import xml.etree.ElementTree as ET
tree = ET.parse("path_to_xml_file")
root = tree.getroot()

You can iterate over all children nodes of root:

for child in root.iter():
    if child.tag == 'clientid':
        print(child.tag, child.text.strip())

Children are nested, and we can access specific child nodes by index, so root[0][1] should work (as long as the indices are correct).

Upvotes: 6

Related Questions