Codevalley
Codevalley

Reputation: 4641

How to read the value of an XML tag having attributes using minidom

I have an XML which looks like this.

<nb:myelement param='oxygen'>Value</nb:element>

I am using the following python code.

for sub in dom.getElementsByTagName('nb:myelement'):
   if(sub.getAttributeNode("param").nodeValue == 'oxygen'):
      value = sub.getElementsByTagName('nb:myelement')[0].toxml()

But the last line is throwing an index out of range execption. How do I get the 'value' enclosed by a tag that also has attributes?

Upvotes: 1

Views: 444

Answers (1)

pieceofgraphite
pieceofgraphite

Reputation: 26

There are no other nb:myelement elements in your nb:myelement. So last line is really

[][0].toxml()

If there is only text in nb:myelement, just use text node of this element

sub.firstChild.nodeValue

Upvotes: 1

Related Questions