473,779 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2410
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.fi rstChild.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.parseSt ring(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(resul t_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.fro mstring("""<par a 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
2421
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 weaver/yarn pattern--in order to traverse the tree that is being compressed or decompressed. I do not know of any other interesting application of the weaver/yarn pattern, but it seems like a very suitable subject of study for different...
2
1688
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 set of data © # and functions as a class, and name it xxx © class xxx:
10
3691
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. Andrew dalke@dalkescientific.com
9
2035
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 pythonic approach. One thing I've been seeing suggested a lot lately is that running jobs in separate processes, to make it easy to use the latest multiprocessor machines. Makes a lot of sense to me, those processors are going to be more and...
112
13865
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 because they smell "Javaish." But now my code base is expanding and I'm beginning to appreciate the wisdom behind them. I welcome example code and illustrations.
0
1984
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 ( +4) / 291 closed ( +4) / 553 total ( +8) New / Reopened Patches ______________________
162
10300
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 prefix them with 2 underscores, but I hate prefixing my vars, I'd rather add a keyword before it. Python advertises himself as a full OOP language, but why does it miss one of the basic principles of OOP? Will it ever be added to python?
0
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10306
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10075
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8961
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.