[Tutor] A somewhat easier way to parse XML

David Rock david at graniteweb.com
Wed Jan 19 17:55:17 CET 2005


* Max Noel <maxnoel_fr at yahoo.fr> [2005-01-19 11:48]:
> 
> On Jan 19, 2005, at 03:58, David Rock wrote:
> 
> >For me, it seems that the way you are supposed to interact with an XML
> >DOM is to already know what you are looking for, and in theory, you
> >_should_ know ;-)
> 
> 	Indeed. The problem is, even if I know what I'm looking for, the  
> problem remains that given the following document,
> 
> <foo>
> 	<bar>baz</bar>
> </foo>
> 
> 	If I want to get "baz", the command is (assuming a DOM object has 
> 	been  created):
> 
> doc.documentElement.getElementsByTagName("bar")[0].childNodes[0].nodeVal 
> ue
> 
> 	Quoting from memory there, it may not be entirely correct. However,  
> the command has more characters than the document itself. Somehow I  
> feel it'd be a bit more elegant to use:
> 
> doc["bar"]
> 
> (or depending on the implementation, doc["foo"]["bar"])
> 
> 	Don't you think?

Absolutely. That is exactly what I was hoping for, too. ElementTree
comes close, but even that can be a bit unwieldy because of the
multi-dimentional array you end up with. Still, if you know the data,

doc[0][0] is a lot easier than doc.documentElement...nodeValue

-- 
David Rock
david at graniteweb.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20050119/b03af9fa/attachment-0001.pgp


More information about the Tutor mailing list