loads() – Load xml content to python object.

A simple xml parse and build library.

lazyxml.loads(content, encoding=None, unescape=False, strip_root=True, strip_attr=True, strip=True, errors='strict')

Load xml content to python object.

>>> import lazyxml
>>> xml = '<demo><foo>foo</foo><bar>bar</bar></demo>'
>>> lazyxml.loads(xml)
{'bar': 'bar', 'foo': 'foo'}
>>> xml = '<demo><foo>foo</foo><bar>bar</bar></demo>'
>>> lazyxml.loads(xml, strip_root=False)
{'demo': {'bar': 'bar', 'foo': 'foo'}}
>>> xml = '<demo><foo>foo</foo><bar>1</bar><bar>2</bar></demo>'
>>> lazyxml.loads(xml)
{'bar': ['1', '2'], 'foo': 'foo'}
>>> xml = '<root xmlns:h="http://www.w3.org/TR/html4/">&lt;demo&gt;&lt;foo&gt;foo&lt;/foo&gt;&lt;bar&gt;bar&lt;/bar&gt;&lt;/demo&gt;</root>'
>>> lazyxml.loads(xml, unescape=True, strip_root=False)
{'root': {'demo': {'bar': 'bar', 'foo': 'foo'}}}
Parameters:
  • content (str) – xml content.
  • encoding (str) – xml content encoding. if not set, will guess from xml header declare if possible.
  • unescape (bool) – whether to unescape xml html entity character. Default to False.
  • strip_root (bool) – whether to strip root. Default to True.
  • strip_attr (bool) – whether to strip tag attrs. Default to True.
  • strip (bool) – whether to strip whitespace. Default to True.
  • errors (string) – the xml content decode error handling scheme. Default to strict.
Return type:

dict

Changed in version 1.2.1: The strip_attr option supported to decide whether return the element attributes for parse result.