REALLY simple xml reader

Ricardo Aráoz ricaraoz at gmail.com
Thu Jan 31 07:39:16 EST 2008


Diez B. Roggisch wrote:
> Ricardo Aráoz schrieb:
>> Thanks Ivan, it seems a elegant API, and easy to use.
>> I tried to play a little with it but unfortunately could not get it off
>> the ground. I kept getting
>>>>> root = et.fromstring(doc)
>> Traceback (most recent call last):
>>   File "<input>", line 1, in <module>
>>   File "E:\Python25\lib\xml\etree\ElementTree.py", line 963, in XML
>>     parser.feed(text)
>>   File "E:\Python25\lib\xml\etree\ElementTree.py", line 1245, in feed
>>     self._parser.Parse(data, 0)
>> ExpatError: XML or text declaration not at start of entity: line 2, column 0
> 
> That's a problem in your XML not being XML. Has nothing to do with 
> element-tree - as one sees from the error-message "ExpatError". If you 
> show it to us, we might see why.
> 

Sure,

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>

  <debit category="withdrawal">
    <amount>40</amount>
    <date><year>2002</year><month>1</month><day>8</day></date>
    <description>pocket money</description>
  </debit>

  <debit category="savings">
    <amount>25</amount>
    <date><year>2002</year><month>1</month><day>8</day></date>
  </debit>

  <debit category="medical" check="855">
    <amount>188.20</amount>
    <date><year>2002</year><month>1</month><day>8</day></date>
    <payto>Boston Endodontics</payto>
    <description>cavity</description>
  </debit>

  <debit category="supplies">
    <amount>10.58</amount>
    <date><year>2002</year><month>1</month><day>10</day></date>
    <payto>Exxon Saugus</payto>
    <description>gasoline</description>
  </debit>

  <debit category="car">
    <amount>909.56</amount>
    <date><year>2002</year><month>1</month><day>14</day></date>
    <payto>Honda North</payto>
    <description>car repairs</description>
  </debit>

  <debit category="food">
    <amount>24.30</amount>
    <date><year>2002</year><month>1</month><day>15</day></date>
    <payto>Johnny Rockets</payto>
    <description>lunch</description>
  </debit>
</checkbook>
"""
I thought this was proper XML as it comes straight out from an O'Reilly
XML book.

Cheers



More information about the Python-list mailing list