Newbie needs help extracting data from XML

Alan Kennedy alanmk at hotmail.com
Thu Dec 29 06:58:21 EST 2005


[Rodney]
> Im a Python newbie and am trying to get the data out of a series of XML 
> files.

As Paul McGuire already noted, it's unusual to extract information from 
a SOAP message this way: it is more usual to use a SOAP toolkit to do 
the job for you.

But, assuming that you know what you're doing, and that you're doing it 
for good reasons, here's a snippet that uses xpath to do what you want.

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
document = """\
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
   xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
   xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
   xmlns:tns="http://www.ExchangeNetwork.net/schema/v1.0/node.wsdl"
   xmlns:types="http://www.ExchangeNetwork.net/schema/v1.0/node.wsdl"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Header>
     <wsu:Timestamp
       xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility">
       <wsu:Created>2005-12-28T05:59:38Z</wsu:Created>
       <wsu:Expires>2005-12-28T06:04:38Z</wsu:Expires>
     </wsu:Timestamp>
   </soap:Header>
   <soap:Body
     soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
     <q1:NodePingResponse
       xmlns:q1="http://www.ExchangeNetwork.net/schema/v1.0/node.xsd">
       <return xsi:type="xsd:string">Ready</return>
     </q1:NodePingResponse>
   </soap:Body>
</soap:Envelope>
"""

import xml.dom.minidom
import xml.xpath

#dom_tree = xml.dom.minidom.parse('my_xml_file.xml')
dom_tree = xml.dom.minidom.parseString(document)
return_node = xml.xpath.Evaluate('//return', dom_tree)[0]
print "Return status is: '%s'" % return_node.childNodes[0].nodeValue
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

You have to install PyXML to get xpath support: http://pyxml.sf.net

There are other ways to do it, e.g. using ElementTree, but I'll leave it 
to others to suggest the best way to do that.

HTH,

-- 
alan kennedy
------------------------------------------------------
email alan:              http://xhaus.com/contact/alan



More information about the Python-list mailing list