title: lxml convert element to elementtree
body: <p>The following test code reads a file, and using <code>lxml.html</code> generates the leaf nodes of the DOM/Graph for the page.</p>
  <p>However, I'm also trying to figure out how to get the input from a &quot;string&quot;. Using:</p>
  <pre><code> lxml.html.fromstring(s)
  </code></pre>
  <p>doesn't work, as this generates an <code>Element</code> as opposed to an <code>ElementTree</code>.</p>
  <p>So, I'm trying to figure out <strong>how to convert an element to an ElementTree</strong>.</p>
  <h3>[my test code]</h3>
  <pre><code>import lxml.html
  from lxml import etree    # trying this to see if needed 
                            # to convert from element to elementtree
  
  
    #cmd='cat osu_test.txt'
    cmd='cat o2.txt'
    proc=subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE)
    s=proc.communicate()[0].strip()
  
    # s contains HTML not XML text
    #doc = lxml.html.parse(s)
    doc = lxml.html.parse('osu_test.txt')
    doc1 = lxml.html.fromstring(s)
  
    for node in doc.iter():
    if len(node) == 0:
       print &quot;aaa &quot;,node.tag, doc.getpath(node)
       #print &quot;aaa &quot;,node.tag
  
    nt = etree.ElementTree(doc1)        &lt;&lt;&lt;&lt;&lt; doesn't work.. so what will??
    for node in nt.iter():
    if len(node) == 0:
       print &quot;aaa &quot;,node.tag, doc.getpath(node)
       #print &quot;aaa &quot;,node.tag
  </code></pre>
  <hr />
  <h3>UPDATE 1:</h3>
  <p>(parsing html instead of xml)
  Added the changes suggested by Abbas. got the following errs:</p>
  <pre><code>    doc1 = etree.fromstring(s)
    File &quot;lxml.etree.pyx&quot;, line 2532, in lxml.etree.fromstring (src/lxml/lxml.etree.c:48621)
    File &quot;parser.pxi&quot;, line 1545, in lxml.etree._parseMemoryDocument (src/lxml/lxml.etree.c:72232)
    File &quot;parser.pxi&quot;, line 1424, in lxml.etree._parseDoc (src/lxml/lxml.etree.c:71093)
    File &quot;parser.pxi&quot;, line 938, in lxml.etree._BaseParser._parseDoc (src/lxml/lxml.etree.c:67862)
    File &quot;parser.pxi&quot;, line 539, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:64244)
    File &quot;parser.pxi&quot;, line 625, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:65165)
    File &quot;parser.pxi&quot;, line 565, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:64508)
  lxml.etree.XMLSyntaxError: Entity 'nbsp' not defined, line 48, column 220
  </code></pre>
  <h3>UPDATE 2:</h3>
  <p>Managed to get the test working. I'm not exactly sure why. If someone with py chops wants to provide an explanation, that would help future people who stumble on this.</p>
  <pre><code>from cStringIO import StringIO
  from lxml.html import parse
  
  doc1 = parse(StringIO(s))
  
  for node in doc1.iter():
      if len(node) == 0:
          print &quot;aaa &quot;, node.tag, doc1.getpath(node)
  </code></pre>
  <p>It appears that the StringIO module/class implements IO functionality which satisfies what the parse package needs to go ahead and process the input string for the test html. similar to what casting provides in other languages perhaps...</p>