load() – Load xml content from file and convert to python object.

A simple xml parse and build library.

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

Load xml content from file and convert to python object.

>>> import lazyxml
>>> with open('demo.xml', 'rb') as fp:
>>>     lazyxml.load(fp)
>>> from cStringIO import StringIO
>>> buf = StringIO('<?xml version="1.0" encoding="utf-8"?><demo><foo><![CDATA[<foo>]]></foo><bar><![CDATA[1]]></bar><bar><![CDATA[2]]></bar></demo>')
>>> lazyxml.load(buf)
{'bar': ['1', '2'], 'foo': '<foo>'}
>>> buf.close()
Parameters:
  • fp – a file or file-like object that support .read() to read the 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.