REALLY simple xml reader

Stefan Behnel stefan.behnel-n05pAM at web.de
Tue Jan 29 09:17:55 EST 2008


Hi,

Ricardo Aráoz wrote:
> I don't know zit about xml, but I might need to, and I am saving the
> thread for when I need it. So I looked around and found some 'real'
> XML document (see below). The question is, how to access <amount>s from
> <debit>s (any category) but not <deposit>s.
> 
> doc = """
> <?xml version="1.0"?>
> <checkbook balance-start="2460.62">
> <title>expenses: january 2002</title>
> 
>   <debit category="clothes">
>     <amount>31.19</amount>
>     <date><year>2002</year><month>1</month><day>3</day></date>
>     <payto>Walking Store</payto>
>     <description>shoes</description>
>   </debit>
> 
>   <deposit category="salary">
>     <amount>1549.58</amount>
>     <date><year>2002</year><month>1</month><day>7</day></date>
>     <payor>Bob's Bolts</payor>
>   </deposit>
[...]
> </checkbook>
> """

Sure, no problem. Just use the XPath expression "//debit/amount", or maybe
"/checkbook/credit/amount", if you prefer. This is basically tree traversal,
so you can check the parents and the children as you see fit.

Stefan



More information about the Python-list mailing list