[XML-SIG] Does anyone do DOM navigation anymore?
uche.ogbuji at fourthought.com
Tue Jul 20 14:38:10 CEST 2004
On Mon, 2004-07-05 at 21:40, Derek Fountain wrote:
> On Monday 05 July 2004 23:34, you wrote:
> > I use the DOM navigation all the time.
> > I do not know about XPATH so I cannot say if I would use that more than
> > DOM.
> How do you cope with the fact that documents are to some extent unpredictable?
> Do you make heavy use of the methods/attributes which allow you to "feel
> around" to see what's coming (hasChildNodes, nodeType and so on)? Or do you
> only use DOM when you can be guaranteed about the structure of the document,
> and you therefore know that, for example,
> currentNode.firstChild.firstChild.lastChild.firstChild.nodeValue will give
> you text you're after?
> I'm starting to wonder if I've been doing the DOM right, as it were. It seems
> to me that when you don't know in advance how many children an element has,
> and you have to start feeling your way around, it makes the code rather
> fragile. Someone adds an extra child where your test cases never had one, and
> boom, the code breaks. Perhaps people code to the DTD, rather than any one
> document itself?
Tom already mentioned getElementsbyTagNameNS. I sually use XPath.
But if you're on Python 2.2. or more recent, you can cook up a lot of
neat patterns with generators which avoid the problems you mentioned:
Uche Ogbuji Fourthought, Inc.
http://uche.ogbuji.net http://4Suite.org http://fourthought.com
Perspective on XML: Steady steps spell success with Google - http://www.adtmag.com/article.asp?id=9663
Use XML namespaces with care - http://www-106.ibm.com/developerworks/xml/library/x-namcar.html
Managing XML libraries - http://www.adtmag.com/article.asp?id=9160
Commentary on "Objects. Encapsulation. XML?" - http://www.adtmag.com/article.asp?id=9090
A survey of XML standards - http://www-106.ibm.com/developerworks/xml/library/x-stand4/
More information about the XML-SIG