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

T

Tom Anderson

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
 
D

Diez B. Roggisch

Tom said:
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
 
U

uche.ogbuji

"""
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/developerworks/edu/x-dw-x-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/developerworks/xml/library/x-think31.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top