What is the difference between XML() and fromstring() in etree?
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
Am 20. Dezember 2017 15:20:35 MEZ schrieb Peng Yu:
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? http://lxml.de/tutorial.html#the-fromstring-function
I still don't see any practical difference other than the function names different. On Wed, Dec 20, 2017 at 11:33 AM Stefan Behnel <stefan_ml@behnel.de> wrote:
Am 20. Dezember 2017 15:20:35 MEZ schrieb Peng Yu:
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? http://lxml.de/tutorial.html#the-fromstring-function
Mailing list for the lxml Python XML toolkit - http://lxml.de/ lxml@lxml.de https://mailman-mail5.webfaction.com/listinfo/lxml
-- Regards, Peng
Peng Yu schrieb am 20.12.2017 um 20:30:
On Wed, Dec 20, 2017 at 11:33 AM Stefan Behnel wrote:
Am 20. Dezember 2017 15:20:35 MEZ schrieb Peng Yu: >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? http://lxml.de/tutorial.html#the-fromstring-function
I still don't see any practical difference other than the function names different.
Quoting from the above link: """" The XML() function behaves like the fromstring() function, but is commonly used to write XML literals right into the source """ Meaning, the difference is in use cases and their readability, not in functionality. If you have an XML literal in your code, it looks good to write XML("<root />") If you received a string from somewhere that contains XML, it looks better to write fromstring(data) Stefan
participants (2)
-
Peng Yu
-
Stefan Behnel