xml parsing escape characters

"Martin v. Löwis" martin at v.loewis.de
Thu Jan 20 12:44:25 EST 2005


Luis P. Mendes wrote:
> with:    DataSetNode = stringNode.childNodes[0]
>     print DataSetNode.toxml()
> 
> I get:
> 
> <DataSet>
> ~  <Order>
> ~    <Customer>439</Customer>
> 
> ~  </Order>
> </DataSet>
> _______________________________________________________________-
> 
> so far so good, but when I issue the command:
> 
> print DataSetNode.childNodes[0]
> 
> I get:
> IndexError: tuple index out of range
> 
> Why the error, and why does it return a tuple?

The DataSetNode has no children, because it is not
an Element node, but a Text node. In XML, an element
is denoted by

   <DataSet>...</DataSet>

and *not* by

   <DataSet>...</DataSet>

The latter is just a single string, represented
in XML as a Text node. It does not give you any
hierarchy whatsoever.

As a text node does not have any children, its
childNode members is a empty tuple; accessing
that tuple gives you an IndexError.

Regards,
Martin



More information about the Python-list mailing list