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

M

martijn

H!,

Is it possible to get a <tag:id>value</tag:id> value ?

When I do this:
-----------------------------------------------------
theXML = """<?xml version="1.0"?>
<title>The Fascist Menace</title>
"""
import xml.dom.minidom as dom
doc = dom.parseString(theXML)
print doc.getElementsByTagName('title')[0].toxml()

I get : <title>The Fascist Menace</title> thats oke for me
-----------------------------------------------------

But the xmlfile I must read have other tags:
theXML = """<?xml version="1.0"?>
<title:id>The Fascist Menace</title:id>
<title:name>bla la etc</title:name>
"""

how to get that values ?
I try things like:
print doc.getElementsByTagName('title:id')[0].toxml() <--error

Thanks,
GC-Martijn
 
U

uche.ogbuji

H!,

Is it possible to get a <tag:id>value</tag:id> value ?

When I do this:
-----------------------------------------------------
theXML = """<?xml version="1.0"?>
<title>The Fascist Menace</title>
"""
import xml.dom.minidom as dom
doc = dom.parseString(theXML)
print doc.getElementsByTagName('title')[0].toxml()

I get : <title>The Fascist Menace</title> thats oke for me
-----------------------------------------------------

But the xmlfile I must read have other tags:
theXML = """<?xml version="1.0"?>
<title:id>The Fascist Menace</title:id>
<title:name>bla la etc</title:name>
"""

how to get that values ?
I try things like:
print doc.getElementsByTagName('title:id')[0].toxml() <--error

Addressing your general question, unfortunately you're a bit stuck.
Minidom is rather confused about whether or not it's a namespace aware
library. Addressing your specific example, I strongly advise you not
to use documents that are not well-formed according to Namespaces 1.0.
Your second example is a well-formed XML 1.0 external parsed entity,
but not a well-formed XML 1.0 document entity, because it has multiple
elements at document level. It's also not well-formed according to
XMLNS 1.0 unless you declare the "title" prefix. You will not be able
to use a non XMLNS 1.0 document with most XML technologies, including
XSLT, WXS, RELAX NG, etc.

If you have indeed declared a namespace and are just giving us a very
bad example, use:

print doc.getElementsByTagNameNS(title_namespace, 'id')
 
M

martijn

I'm newbie with that xml stuff.

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>
<extension>
<eurid:ext>
<eurid:result>
<eurid:msg>OK</dnsbe:msg>
</eurid:result>
</eurid:ext>
</extension>
<trID>
<clTRID>clientref-12310026</clTRID>
<svTRID>eurid-1589</svTRID>
</trID>
</response>
</epp>
----------------
//<result code="1500">
//<msg>Command completed successfully; ending session</msg>

what is the official/best way to handle/parse such xml response ?

Thats maybe a better question
 
A

Albert Leibbrandt

I'm newbie with that xml stuff.

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>
<extension>
<eurid:ext>
<eurid:result>
<eurid:msg>OK</dnsbe:msg>
</eurid:result>
</eurid:ext>
</extension>
<trID>
<clTRID>clientref-12310026</clTRID>
<svTRID>eurid-1589</svTRID>
</trID>
</response>
</epp>
----------------
//<result code="1500">
//<msg>Command completed successfully; ending session</msg>

what is the official/best way to handle/parse such xml response ?

Thats maybe a better question
try something like this:

import xml.dom.minidom as dom
doc = dom.parseString("""<?xml version="1.0"?>
<title>The Fascist Menace</title>""")
print doc.getElementsByTagName('title')[0].childNodes[0].data
 
U

uche.ogbuji

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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top