outis
outis

Reputation: 77420

What's the functional difference between `etree.fromstring()` and `etree.XML()` in lxml?

lxml offers a few different functions to parse strings. Two of them, etree.fromstring() and etree.XML(), seem very similar. The docstring for the former says it's for parsing "strings", while the latter "string constants". Additionally, XML()'s docstring states:

This function can be used to embed "XML literals" in Python code, [...]

What's the functional difference between these functions? When should one be used over the other?

Upvotes: 9

Views: 2159

Answers (1)

outis
outis

Reputation: 77420

Looking at the source code, for XML() and fromstring(), the former has this extra snippet of code:

if parser is None:
    parser = __GLOBAL_PARSER_CONTEXT.getDefaultParser()
    if not isinstance(parser, XMLParser):
        parser = __DEFAULT_XML_PARSER

They thus differ in side effects: XML() only uses the default XML parser as the default parser. If the default parser were changed to a non-XMLParser, XML() will ignore it.

etree.set_default_parser(etree.HTMLParser())
etree.tostring(etree.fromstring("<root/>"))
# b'<html><body><root/></body></html>'
etree.tostring(etree.XML("<root/>"))
# b'<root/>'

Upvotes: 8

Related Questions