
I am trying to find out the difference between XML() and fromstring() in etree. But it seems that they are the same. Does anybody know if what is the main difference between them? Thanks. $ cat ./main.py #!/usr/bin/env python # vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 fileencoding=utf-8: from lxml import etree tree = etree.fromstring('<a>text</a>') print type(tree) tree = etree.XML('<a>text</a>') print type(tree) $ ./main.py <type 'lxml.etree._Element'> <type 'lxml.etree._Element'> Help on cython_function_or_method in module lxml.etree: XML(...) XML(text, parser=None, base_url=None) Parses an XML document or fragment from a string constant. Returns the root node (or the result returned by a parser target). This function can be used to embed "XML literals" in Python code, like in >>> root = XML("<root><test/></root>") >>> print(root.tag) root To override the parser with a different ``XMLParser`` you can pass it to the ``parser`` keyword argument. The ``base_url`` keyword argument allows to set the original base URL of the document to support relative Paths when looking up external entities (DTD, XInclude, ...). Help on cython_function_or_method in module lxml.etree: fromstring(...) fromstring(text, parser=None, base_url=None) Parses an XML document or fragment from a string. Returns the root node (or the result returned by a parser target). To override the default parser with a different parser you can pass it to the ``parser`` keyword argument. The ``base_url`` keyword argument allows to set the original base URL of the document to support relative Paths when looking up external entities (DTD, XInclude, ...). -- Regards, Peng