[Tutor] Extracting xml text

Stefan Behnel stefan_ml at behnel.de
Sun Jun 20 16:15:55 CEST 2010


T.R. D., 20.06.2010 16:04:
> I decided to go with iterparse but trying the simple example in the python
> interpreter led to an error (see below) and when I tried this with a much
> larger xml sample, it seemed to print the full elements, not the specific
> values of the element. For example, given what I entered in the python
> interpreter, the result would have been the full xml example, and not
> "Reminder" "Don't forget me this weekend".
>
> Did I do something wrong in the sample below?

Yes. You didn't follow the example and you didn't read the docs.


> >>> from xml.etree.cElementTree import iterparse
> >>> sample = '''\
> ...<note>
> ...<to>Tove</to>
> ...<from>Jani</from>
> ...<heading>Reminder</heading>
> ...<body>Don't forget me this weekend!</body>
> ...</note>
> ... '''
> >>> print sample
> <note>
>      <to>Tove</to>
>      <from>Jani</from>
>      <heading>Reminder</heading>
>      <body>Don't forget me this weekend!</body>
> </note>
>
> >>> for event, elem in iterparse(sample):
> ...     if elem.tag == 'note':
> ...             print elem.findtext('heading'), elem.findtext('body')
> ...             elem.clear()
> ...
> ...
> Traceback (most recent call last):
>    File "<stdin>", line 1, in<module>
>    File "<string>", line 52, in __init__
> IOError: [Errno 2] No such file or directory:
> "<note>\n\t<to>Tove</to>\n\t<from>Jani</from>\n\t<heading>Reminder</heading>\n\t<body>Don't
> forget me this weekend!</body>\n</note>\n"

That's pretty telling, isn't it? It's looking for the file of which you 
passed the filename. Since you passed XML string data as filename here, it 
can't find it.

Wrap the data in a StringIO.StringIO instance instead (or io.BytesIO in 
Python 2.6 and later) and pass that into iterparse().

Stefan



More information about the Tutor mailing list