'lxml.etree._Element' object has no attribute 'write' ??? (PYTHON)
Question: 
    from lxml import etree
    
    root = etree.Element('root1') 
    element = etree.SubElement(root, 'element1')
    root.write( 'xmltree.xml' ) 
    

Error:

    
    
    AttributeError: 'lxml.etree._Element' object has no attribute 'write'
    

how can I fix this?


Answer: If you are wanting to save your new xml to a file then `etree.tostring` is the
method to use.

E.g.

    
    
    >>> from lxml import etree
    >>> root = etree.Element('root1')
    >>> element = etree.SubElement(root, 'element1')
    >>> print etree.tostring(root,pretty_print=True) ## Print document
    <root1>
      <element1/>
    </root1>
    >>> with open('xmltree.xml','w') as f: ## Write document to file
    ...   f.write(etree.tostring(root,pretty_print=True))
    ...
    >>>
    

