only a simple xml reader <tag:id>value</tag:id>

uche.ogbuji at gmail.com uche.ogbuji at gmail.com
Sat Feb 11 13:44:48 EST 2006


martijn at gamecreators.nl wrote:
> The only thing I must read is the response I get from a EPP server.
> A response like this:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <epp xmlns=" http://www.eurid.eu/xml/epp/epp-1.0"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:contact="http://www.eurid.eu/xml/epp/contact-1.0"
> xmlns:domain="http://www.eurid.eu/xml/epp/domain-1.0"
> xmlns:eurid="http://www.eurid.eu/xml/epp/eurid-1.0"
> xmlns:nsgroup="http://www.eurid.eu/xml/epp/nsgroup-1.0"
> xsi:schemaLocation="http://www.eurid.eu/xml/epp/epp-1.0 epp-1.0.xsd
> http://www.eurid.eu/xml/epp/contact-1.0 contact-1.0.xsd
> http://www.eurid.eu/xml/epp/domain-1.0 domain-1.0.xsd
> http://www.eurid.eu/xml/epp/eurid-1.0 eurid-1.0.xsd
> http://www.eurid.eu/xml/epp/nsgroup-1.0 nsgroup-1.0.xsd">
> <response>
> <result code="1500">
> <msg>Command completed successfully; ending session</msg>
> </result>
> <resData>
> <domain:appData>
> <domain:name>c-and-a.eu</domain:name>
> <domain:reference> c-and-a_1</domain:reference>
> <domain:code>2565100006029999</domain:code>
> <domain:crDate>2005-11-08T14:51:08.929Z</domain:crDate>
> </domain:appData>
> </resData>
> <!-- SNIP -->
> </response>
> </epp>

So to get the msg, you can do:

print doc.getElementsByTagName('msg')[0].toxml()

But to get the domain:name you have to use the declared namespace:

print
doc.getElementsByTagNameNS('http://www.eurid.eu/xml/epp/domain-1.0',
'name')[0].toxml()

Or you can make life a bit easier with Amara [1]:

import amara
doc = amara.parse(theXML)
print doc.response.result.msg #to get the text content
print doc.response.result.msg.xml() #to get the XML source for that
element
print doc.response.resData.appData.name
print doc.response.resData.appData.name.xml()

[1] http://uche.ogbuji.net/tech/4Suite/amara/

--
Uche Ogbuji                               Fourthought, Inc.
http://uche.ogbuji.net                    http://fourthought.com
http://copia.ogbuji.net                   http://4Suite.org
Articles: http://uche.ogbuji.net/tech/publications/




More information about the Python-list mailing list