ElementTree : parse string input

Fredrik Lundh fredrik at pythonware.com
Thu Jul 6 10:49:19 EDT 2006


rajarshi.guha at gmail.com wrote:

> Hi, recently having discovered ElementTree I'm stumped by a very simple
> problem, which I can't find the answer to.
> 
> I have some XML in a string object. Now the parse() method of
> ElementTree takes a filename or file-like object. So I tried creating a
> StringIO object from the original string and then giving that to
> parse(). But that does not seem to work.

that should work, but the fromstring function (and it's XML alias) is a 
lot easier to use.

     data = "some xml in a string"

     elem = ElementTree.XML(data)

also see the end of this section:

     http://www.effbot.org/zone/element.htm#reading-and-writing-xml-files

</F>




More information about the Python-list mailing list