[lxml-dev] eetree.fromsring() returns Element, expected ElementTree

Ran into something that maybe a bug, or at least isn't clear from the documentation [http://codespeak.net/lxml/api/lxml.etree-module.html#fromstring] because it doesn't mention a return type for etree.fromstring(). I had expected it to behave similar to etree.parse(). Currently I have a work-around of: tree = etree.ElementTree(etree.fromstring(xml_data)) See below for simple test, and output. ------- #!/usr/bin/python import sys,StringIO from lxml import etree print "lxml.etree: ", etree.LXML_VERSION print "libxml used: ", etree.LIBXML_VERSION print "libxml compiled: ", etree.LIBXML_COMPILED_VERSION print "libxslt used: ", etree.LIBXSLT_VERSION print "libxslt compiled: ", etree.LIBXSLT_COMPILED_VERSION some_xml_data = "<root>data</root>" tree1=etree.fromstring(some_xml_data) tree2=etree.parse(StringIO.StringIO(some_xml_data)) print type(tree1) print type(tree2) -------------- lxml.etree: (2, 1, 5, 0) libxml used: (2, 7, 3) libxml compiled: (2, 6, 32) libxslt used: (1, 1, 24) libxslt compiled: (1, 1, 24) <type 'lxml.etree._Element'> <type 'lxml.etree._ElementTree'> 'lxml.etree._Element' object has no attribute 'write' -- David Gardner Pipeline Tools Programmer, "Sid the Science Kid" Jim Henson Creature Shop dgardner@creatureshop.com

Woops sorry, I added a bit to the test, before re-pasting, the test code should be: --------------------- #!/usr/bin/python import sys,StringIO from lxml import etree print "lxml.etree: ", etree.LXML_VERSION print "libxml used: ", etree.LIBXML_VERSION print "libxml compiled: ", etree.LIBXML_COMPILED_VERSION print "libxslt used: ", etree.LIBXSLT_VERSION print "libxslt compiled: ", etree.LIBXSLT_COMPILED_VERSION some_xml_data = "<root>data</root>" tree1=etree.fromstring(some_xml_data) tree2=etree.parse(StringIO.StringIO(some_xml_data)) print type(tree1) print type(tree2) out1=StringIO.StringIO() out2=StringIO.StringIO() try: tree1.write(out1,pretty_print=True) except Exception,e: print str(e) try: tree2.write(out2,pretty_print=True) except Exception,e: print str(e) ------------------------ lxml.etree: (2, 1, 5, 0) libxml used: (2, 7, 3) libxml compiled: (2, 6, 32) libxslt used: (1, 1, 24) libxslt compiled: (1, 1, 24) <type 'lxml.etree._Element'> <type 'lxml.etree._ElementTree'> 'lxml.etree._Element' object has no attribute 'write' David Gardner wrote:
Ran into something that maybe a bug, or at least isn't clear from the documentation [http://codespeak.net/lxml/api/lxml.etree-module.html#fromstring] because it doesn't mention a return type for etree.fromstring(). I had expected it to behave similar to etree.parse().
Currently I have a work-around of: tree = etree.ElementTree(etree.fromstring(xml_data))
See below for simple test, and output.
------- #!/usr/bin/python
import sys,StringIO from lxml import etree
print "lxml.etree: ", etree.LXML_VERSION print "libxml used: ", etree.LIBXML_VERSION print "libxml compiled: ", etree.LIBXML_COMPILED_VERSION print "libxslt used: ", etree.LIBXSLT_VERSION print "libxslt compiled: ", etree.LIBXSLT_COMPILED_VERSION
some_xml_data = "<root>data</root>"
tree1=etree.fromstring(some_xml_data) tree2=etree.parse(StringIO.StringIO(some_xml_data))
print type(tree1) print type(tree2)
-------------- lxml.etree: (2, 1, 5, 0) libxml used: (2, 7, 3) libxml compiled: (2, 6, 32) libxslt used: (1, 1, 24) libxslt compiled: (1, 1, 24) <type 'lxml.etree._Element'> <type 'lxml.etree._ElementTree'> 'lxml.etree._Element' object has no attribute 'write'
-- David Gardner Pipeline Tools Programmer, "Sid the Science Kid" Jim Henson Creature Shop dgardner@creatureshop.com

Hi, David Gardner wrote:
Ran into something that maybe a bug, or at least isn't clear from the documentation [http://codespeak.net/lxml/api/lxml.etree-module.html#fromstring] because it doesn't mention a return type for etree.fromstring(). I had expected it to behave similar to etree.parse().
Yes, that's a common misconception. Let's see if this works better: https://codespeak.net/viewvc/lxml/trunk/src/lxml/lxml.etree.pyx?r1=63185&r2=64752 The reason for this difference is that fromstring()/XML() is often used for XML fragments, where returning an ElementTree wouldn't make sense. Stefan
participants (2)
-
David Gardner
-
Stefan Behnel