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

uche.ogbuji at gmail.com uche.ogbuji at gmail.com
Fri Jun 10 13:50:43 EDT 2005


"""
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




More information about the Python-list mailing list