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

Getting a DOM element's children by type (STUPID)

P: n/a
Hi,

If i get myself a DOM tree using xml.dom.minidom (or full-fat xml.dom, i
don't mind), is there an easy way to ask a element for its child elements
of a particular type? By 'type' i mean 'having a certain tag'.

This strikes me as a rather common thing to want to do, but i can't see a
method for it; it seems i have to go through elem.childNodes myself,
fishing out the ones i want. Or rather, to write exactly the same
three-line helper function that millions of other people must have written
to do this. Am i missing something blindingly obvious? I'm happy to RTFM
here, but would appreciate a pointer to the appropriate such manual, since
the docs i have to hand are somewhat unenlightening.

Thanks,
tom

--
Just because Congresspeople do it, doesn't mean it's right. -- Ian York
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Tom Anderson wrote:
This strikes me as a rather common thing to want to do, but i can't see
a method for it; it seems i have to go through elem.childNodes myself,
fishing out the ones i want. Or rather, to write exactly the same
three-line helper function that millions of other people must have
written to do this. Am i missing something blindingly obvious? I'm happy
to RTFM here, but would appreciate a pointer to the appropriate such
manual, since the docs i have to hand are somewhat unenlightening.


No RTFM here - it's just that (stupid..) way. Go read up RTFMs on
elementtree or XPath though - they'll do what you want.

Diez
Jul 19 '05 #2

P: n/a
"""
If i get myself a DOM tree using xml.dom.minidom (or full-fat xml.dom,
i
don't mind)
"""

Don't do that. Stick to minidom. The "full" xml.dom from PyXML is
ancient and slow. Of course, there are other, better libraries
available now, too.

"""
is there an easy way to ask a element for its child elements
of a particular type? By 'type' i mean 'having a certain tag'.
"""

You can use list comprehensions[1]. You could use XPath, if you're
willing to use a library that supports XPath.

In Amara[2], this task is trivial. To get all the images in an XHTML
div, you'd simply do:

for img in div.img:
process_img(img)

You access names directly as objects according to their element type
name.

[1] see, e.g., http://www.xml.com/pub/a/2003/01/08/py-xml.html
[2] see http://www.xml.com/pub/a/2005/01/19/amara.html

--
Uche Ogbuji Fourthought, Inc.
http://uche.ogbuji.net http://fourthought.com
http://copia.ogbuji.net http://4Suite.org
Use CSS to display XML, part 2 -
http://www-128.ibm.com/developerwork...xmlcss2-i.html
XML Output with 4Suite & Amara -
http://www.xml.com/pub/a/2005/04/20/py-xml.htmlUse XSLT to prepare XML
for import into OpenOffice Calc -
http://www.ibm.com/developerworks/xml/library/x-oocalc/
Schema standardization for top-down semantic transparency -
http://www-128.ibm.com/developerwork...x-think31.html

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.