ElementTree should parse string and file in teh same way

Paddy paddy3118 at googlemail.com
Mon Dec 31 02:17:34 EST 2007


On Dec 31, 3:42 am, "Peter Pei" <yan... at telus.com> wrote:
> One bad design about elementtree is that it has different ways parsing a
> string and a file, even worse they return different objects:
> 1) When you parse a file, you can simply call parse, which returns a
> elementtree, on which you can then apply xpath;
> 2) To parse a string (xml section), you can call XML or fromstring, but both
> return element instead of elementtree. This alone is bad. To make it worse,
> you have to create an elementtree from this element before you can utilize
> xpath.

I haven't tried this, but you should be able to wrap your text string
so that it looks like a file using the stringio module and pass that
to elementtree:

http://blog.doughellmann.com/2007/04/pymotw-stringio-and-cstringio.html

- Paddy.



More information about the Python-list mailing list