473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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

Feb 8 '06 #1
4 1873

ma*****@gamecreators.nl wrote:
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')

--
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/

Feb 8 '06 #2
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

Feb 8 '06 #3
ma*****@gamecreators.nl wrote:
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

Feb 9 '06 #4
ma*****@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/

Feb 11 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: TR | last post by:
Is it possible with CSS to prevent this wrapping alignment with a checkbox with a nested label? This is the label of the checkbox that wraps beneath it I'd prefer it looked like...
6
by: hsomob1999 | last post by:
so i have a <ul> and I allow the user to append items to it. The problem is that on mozilla the <span class="line"> which is just a line to divide the sections gets overlaped and doesnt move down...
15
by: Frances | last post by:
<html> <head> <script> function doIt() { var list = document.forms.product; var selItem = list.options.value; ^^^^^^^ </head>
1
by: Rafaela K. Azinhal | last post by:
Hi, I'm a newbie and have a question: I have following PivotTable object in a HTML page <html> <body> <object class='ptdrillthrough' classid="clsid:0002E552-0000-0000-C000-000000000046"...
4
by: Neil Zanella | last post by:
Hello, I would like to know what the difference is among the constructs <%= %> for evaluating an expression and displaying the evaluated result on the page and <%# %>. In particular I would like...
3
by: ajaspersonal | last post by:
"i want to change font_style (hyper link<a href...>text</a>) normal to italics.when load a page" this is my problem but that label included in one 'USERCONTROL' This user controls may...
10
by: neverquit | last post by:
hi , Iam Nagesh,Begineer in using Ajax,well i have been using ajax in application, i have faced a problem while placing the responseTEXT into the <div> tag positioned inside the <form> tag iam...
7
by: Max2006 | last post by:
Hi, When I use this tag: <asp:lable . Visible='<%# IsEditable %>' I have to do DataBind so the binding takes place. I am trying to use the following notation to do the similar thing...
5
Claus Mygind
by: Claus Mygind | last post by:
I want to dynamically add a hidden <div> </div> to a document when loading my page. It is kind of a generic scratch pad into which I can load messages and data I may want to display to the user. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.