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

David Gardner dgardner at creatureshop.com
Tue Apr 28 02:03:27 CEST 2009


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 at creatureshop.com




More information about the lxml-dev mailing list