473,320 Members | 2,073 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,320 software developers and data experts.

Python xml.dom, help reading attribute data

Let's say I have the following xml tag:

<para role="success">1</para>

I can't figure out what kind of python xml.dom codes I should invoke to
read the data 1? Any help please?

Thanks
Thierry

Sep 6 '05 #1
3 2385
Thierry Lam wrote:
Let's say I have the following xml tag:

<para role="success">1</para>

I can't figure out what kind of python xml.dom codes I should invoke to
read the data 1? Any help please?

Thanks
Thierry

In [20]: import xml.dom.minidom

In [21]: s = '''<para role="success">1</para>'''

In [22]: x = xml.dom.minidom.parseString(s)

In [23]: print x.firstChild.firstChild.data
1
I doubt this really answers what you're really wanting to ask. And this
is a really really brittle way of trying to parse XML. But it answers
exactly what you're asking. Hope it gives you the start you need. Post
a follow-up when you have more questions.

JMJ
Sep 6 '05 #2
[Thierry Lam]
Let's say I have the following xml tag:

<para role="success">1</para>

I can't figure out what kind of python xml.dom codes I should invoke to
read the data 1? Any help please?


This is job for xpath.

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
from xml import xpath
from xml.dom import minidom

doc = """<para role="success">1</para>"""

mydom = minidom.parseString(doc)
#result_nodes = xpath.Evaluate("/para/text()", mydom)
#result_nodes = xpath.Evaluate("/para[1]/text()", mydom)
result_nodes = xpath.Evaluate("/para[@role='success']/text()", mydom)
for ix, r in enumerate(result_nodes):
print "result %d: %s" % (ix, r)
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Xpath support is a part of the PyXML package, which you can get from here

http://pyxml.sourceforge.net

Xpath tutorials from here

http://www.zvon.org/xxl/XPathTutoria.../examples.html
http://www.w3schools.com/xpath/

there-are-other-ways-to-do-it-but-i-like-xpath-ly'yrs,

--
alan kennedy
------------------------------------------------------
email alan: http://xhaus.com/contact/alan
Sep 6 '05 #3
Thierry Lam wrote:
Let's say I have the following xml tag:

<para role="success">1</para>

I can't figure out what kind of python xml.dom codes I should invoke
to read the data 1? Any help please?

Thanks
Thierry

If you use elementtree:
from elementtree import ElementTree
node = ElementTree.fromstring("""<para role="success">1</para>""")
node.text '1' node.attrib["role"]

'success'
--
Giovanni Bajo
Sep 7 '05 #4

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

Similar topics

1
by: Christian Stork | last post by:
Hello everybody, I am using Python to prototype a compression algorithm for tree-shaped data, e.g., XML data or abstract syntax trees. I use a variant of the visitor design pattern--called...
2
by: Xah Lee | last post by:
© # -*- coding: utf-8 -*- © # Python © © # in Python, one can define a boxed set © # of data and functions, which are © # traditionally known as "class". © © # in the following, we define a...
10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
9
by: corey.coughlin | last post by:
Alright, so I've been following some of the arguments about enhancing parallelism in python, and I've kind of been struck by how hard things still are. It seems like what we really need is a more...
112
by: mystilleef | last post by:
Hello, What is the Pythonic way of implementing getters and setters. I've heard people say the use of accessors is not Pythonic. But why? And what is the alternative? I refrain from using them...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 385 open (+21) / 3790 closed (+21) / 4175 total (+42) Bugs : 1029 open (+43) / 6744 closed (+43) / 7773 total (+86) RFE : 262 open...
162
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.