By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,546 Members | 1,382 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,546 IT Pros & Developers. It's quick & easy.

ElementTree : parse string input

P: n/a
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.

Any pointers to getting ElementTree to parse from a string would be
appreciated (of course I could dump it to a temp file, but that doesn't
seem elegent)

Thanks,
Rajarshi

Jul 6 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ra***********@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.h...ting-xml-files

</F>

Jul 6 '06 #2

P: n/a
On 6 Jul 2006 07:38:10 -0700, ra***********@gmail.com <ra***********@gmail.comwrote:
Any pointers to getting ElementTree to parse from a string would be
appreciated (of course I could dump it to a temp file, but that doesn't
seem elegent)
You can use the "fromstring" method.
Btw, did you looked at cElementTree module ? It claims to be much
'faster' and has very similar api as ElementTree Module.

link: http://effbot.org/zone/celementtree.htm

cheers,
amit.
--
----
Amit Khemka -- onyomo.com
Home Page: www.cse.iitd.ernet.in/~csd00377
Endless the world's turn, endless the sun's Spinning, Endless the quest;
I turn again, back to my own beginning, And here, find rest.
Jul 6 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.