Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Need to get Tags and Values from Dom

Reply
Thread Tools

Need to get Tags and Values from Dom

 
 
TommyVee
Guest
Posts: n/a
 
      05-14-2012
I have a very simple XML document that I need to "walk", and I'm using
xml.dom.minidom. No attributes, just lots of nested tags and associated
values. All I'm looking to do is iterate through each of the highest
sibling nodes, check what the tag is, and process its value accordingly. If
a node has children, same thing - iterate through the nodes, check the tags
and process the values accordingly. I see where each node object has a
"childNodes" attribute, so I can drill down the tree. But what are the node
attributes which indicate Tag and Value? I thought it would have been
nodeName and nodeValue, but that doesn't seem to be. Does anyone know?

Thanks in advance, TommyVee

 
Reply With Quote
 
 
 
 
TommyVee
Guest
Posts: n/a
 
      05-14-2012
"james hedley" wrote in message
news:11852803.89.1337001575700.JavaMail.geo-discussion-forums@vbmd2...

On Monday, 14 May 2012 01:50:23 UTC+1, TommyVee wrote:
> I have a very simple XML document that I need to "walk", and I'm using
> xml.dom.minidom. No attributes, just lots of nested tags and associated
> values. All I'm looking to do is iterate through each of the highest
> sibling nodes, check what the tag is, and process its value accordingly.
> If
> a node has children, same thing - iterate through the nodes, check the
> tags
> and process the values accordingly. I see where each node object has a
> "childNodes" attribute, so I can drill down the tree. But what are the
> node
> attributes which indicate Tag and Value? I thought it would have been
> nodeName and nodeValue, but that doesn't seem to be. Does anyone know?
>
> Thanks in advance, TommyVee


Ah maybe you're confused about how text nodes work in minidom. Every element
will have a nodeName attribute (not callable) but if you try el.nodeValue on
a text node you get None. That's because the text is represented by a child
node with nodeName '#text', so you want (el.nodeName,
el.firstChild.nodeValue).

General tips - try the docs:
http://docs.python.org/library/xml.dom.minidom.html
and also use dir() a lot on objects when you're learning a new api.

Hope that helps. Disclaimer: haven't used minidom in anger for some time.

Confused? That's an understatement. Part of the problem is that it's been
a long time since I learned DOM and now I'm trying to cram to get this
program done.

Anyway, your suggestion to access el.firstChild.nodeValue did the trick.

Thanks

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JSP Custom Tags as attribute values for other custom tags Dave Java 0 08-14-2006 02:21 PM
What is the difference between DOM Level 1 and DOM Level 2. mike XML 1 11-20-2004 03:19 PM
Difference between pure DOM and JAXP over DOM ?? Thorsten Meininger XML 0 07-28-2004 08:51 AM
Difference between pure DOM and JAXP over DOM ?? Thorsten Meininger Java 0 07-28-2004 08:51 AM
RegEx to find CFML tags nested in HTML tags Dean H. Saxe Perl 0 01-03-2004 06:11 PM



Advertisments