Re: Need to get Tags and Values from Dom
TommyVee, 14.05.2012 02:50: 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? Use the xml.etree.ElementTree module instead. It makes this kind of work really easy, e.g.: import xml.etree.cElementTree as ET # using fast C accelerator module root = ET.parse(afile.xml).getroot() for child in root: if child.tag == 'abc': print(abc tag found) else: print(other tag found) There's also an incremental iterparse() function, in case your documents are large. Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: Need to get Tags and Values from Dom
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Need to get Tags and Values from Dom
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Need to get Tags and Values from Dom
TommyVee, 15.05.2012 01:51: 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. Thus my recommendation to use ElementTree. Why go the complicated route when you can just get your code working without having to learn all of this again? Stefan -- http://mail.python.org/mailman/listinfo/python-list
Need to get Tags and Values from Dom
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Need to get Tags and Values from Dom
On Mon, May 14, 2012 at 6:20 AM, TommyVee x...@xx.xxx 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? A sample of the document you are trying to parse will help people answer. -- regards, kushal -- http://mail.python.org/mailman/listinfo/python-list