473,809 Members | 2,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
2 1789
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
"""
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2302
by: Jean-Christophe Michel | last post by:
Hi, In a complex merging of two (non ordered) xml files i need to keep track of the elements of the second tree that were already merged with first tree, to copy only unused elements at the end. I tried two solutions: * first is to 'substract' the used element from existing tree
3
2095
by: Wim Roffal | last post by:
I am having a <div> with <a>s and <div>s inside it. I am trying to read in all the elements in the right order. With mydiv.getElementsByTagName("a") I can get the elements of a specific tag like "a", but I want all types. I tried getChildNodes but that doesn't seem to be widely supported. childNodes seemed to do something but I couldn't get it working when I wanted to browse through the elements.
6
13381
by: Luke Dalessandro | last post by:
I'm not sure if this is the correct forum for platform specific (Mozilla/Firefox) javascript problems, so just shout and point me to the correct newsgroup if I'm being bad. Here's the deal... html file (generated using .NET 2.0 beta2): <form method="post" action="Test2.aspx" id="form1">
5
1714
by: Arturius mac Aidan | last post by:
I'm not sure if this has been addressed in more recent developments, but I'm reading the MathML specification which states "For example, it is not possible in XML to specify that the first child be interpreted one way, and the second in another." Is there a compelling reason that XML does not support that capability? Has it been fixed? I recall attempting to create my own schemas a couple years ago, and finding this limitation. For my...
1
1765
by: jrmsmo | last post by:
Hi there, I have an interesting problem that maybe you pros can suggest how I solve. I'm working with a third party program that serializes an XML document (it was obviously not designed with schema in mind). I created a schema from this document. It works fine. Except for some unknown reason, in a small part of the XML document, this program switches the order around, and of course the validator I built then fails. Its always the same two...
12
2022
by: Sunny | last post by:
Hi All, I have a serious issue regarding classes scope and visibility. In my application, i have a class name "TextFile", and also a few other classes like "TotalWords", "TotalLines" and etc.., which are suppose to describe the structure of my main TextFile class. Also i have created some custom collection classes, which only take items of the types, they are designed for. Now the problem is that I want my element classes, like...
7
2141
by: marc | last post by:
Dear Sirs, Any idea how I can work this out.? In my document I have a table row with an unknown number and type of elements. I want to make a set of name-value pairs contained in this table row, while I do not know the names of the elements in advance. Is there something like "give me all children of this table row"?
3
27600
by: Andrej Pavlovic | last post by:
Hi, Please look at the following HTML snippet: <body> some text <p>more text</p> even more text </body>
5
2266
by: yawnmoth | last post by:
I'm trying to get a list of all the input elements of a form tag and am having some difficulty doing so. First, here's my XHTML: <div> <form action=""> <input type="text" name="a" /> </div> <div> <input type="text" name="b" /> </div>
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10378
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10121
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.