By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 1,960 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

xml getElementsByTagName w/o recursion?

P: n/a
Hello,

this is perhaps not directly a python question, but how do I get a
direct child element without going down the tree and possibly find
another element with the same name there?
<foo>
<bar>
<name>This is the interesting part</name>
<baz>
<something>...</something>
<name>this is what I DO NOT want</nameA
</baz>
</bar>
</foo>

I am using xml.dom.minidom and I was thinking along the line of

....
fooNode.childNodes.getNamedNode("name")
....
but that doesn't work..

is the only solution to write an own helper function that iterates the
child nodes and get the correct one?

/Simon
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
In article <ab**************************@posting.google.com >, sd******@abo.fi
says...
Hello,

this is perhaps not directly a python question, but how do I get a
direct child element without going down the tree and possibly find
another element with the same name there?
<foo>
<bar>
<name>This is the interesting part</name>
<baz>
<something>...</something>
<name>this is what I DO NOT want</nameA
</baz>
</bar>
</foo>

I am using xml.dom.minidom and I was thinking along the line of

...
fooNode.childNodes.getNamedNode("name")
...
but that doesn't work..

is the only solution to write an own helper function that iterates the
child nodes and get the correct one?

/Simon

You need to loop through the children of the node and build a list of the nodes
of interest.

Bill R
Jul 18 '05 #2

P: n/a
Simon Dahlbacka wrote:
this is perhaps not directly a python question, but how do I get a
direct child element without going down the tree and possibly find
another element with the same name there?
Install PyXML and use xml.xpath.Evaluate().
<foo>
<bar>
<name>This is the interesting part</name>
<baz>
<something>...</something>
<name>this is what I DO NOT want</nameA
</baz>
</bar>
</foo>

I am using xml.dom.minidom and I was thinking along the line of

...
fooNode.childNodes.getNamedNode("name")
...
but that doesn't work..


# Find all of the <name> children of <bar>:
results = xml.xpath.Evaluate( "/foo/bar/child::name", fooNode )

# xml.xpath.Evaluate() returns a list of results, or []
nameNode = results[0]

Or, if you didn't know what the tags were but you still needed the first
<name> element:

results = xml.xpath.Evaluate( "/descendant::name[position()=1]", fooNode )
nameNode = results[0]

XPath (http://www.w3c.org/TR/xpath) can be a bit tricky to pick up, but it's
_very_ powerful for navigating a DOM tree.

--
Chris Herborth ch****@cryptocard.com
Documentation Overlord, CRYPTOCard Corp. http://www.cryptocard.com/
Never send a monster to do the work of an evil scientist.
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.