dump() – Dump python object to file.

A simple xml parse and build library.

lazyxml.dump(obj, fp, encoding=None, header_declare=True, version=None, root=None, cdata=True, indent=None, ksort=False, reverse=False, errors='strict', hasattr=False, attrkey=None, valuekey=None)

Dump python object to file.

>>> import lazyxml
>>> data = {'demo': {'foo': 1, 'bar': 2}}
>>> lazyxml.dump(data, 'dump.xml')
>>> with open('dump-fp.xml', 'w') as fp:
>>>     lazyxml.dump(data, fp)
>>> from cStringIO import StringIO
>>> data = {'demo': {'foo': 1, 'bar': 2}}
>>> buf = StringIO()
>>> lazyxml.dump(data, buf)
>>> buf.getvalue()
<?xml version="1.0" encoding="utf-8"?><demo><foo><![CDATA[1]]></foo><bar><![CDATA[2]]></bar></demo>
>>> buf.close()
Parameters:
  • obj – data for dump to xml.
  • fp – a filename or a file or file-like object that support .write() to write the xml content.
  • encoding (str) – xml content encoding. if not set, consts.Default.ENCODING used.
  • header_declare (bool) – declare xml header. Default to True.
  • version (str) – xml version. if not set, consts.Default.VERSION used.
  • root (str) – xml root. Default to None.
  • cdata (bool) – use cdata. Default to True.
  • indent (str) – xml pretty indent. Default to None.
  • ksort (bool) – sort xml element keys. Default to False.
  • reverse (bool) – sort xml element keys but reverse. Default to False.
  • errors (str) – xml content decode error handling scheme. Default to strict.
  • hasattr (bool) – data element has attributes. Default to False.
  • attrkey (str) – element tag attribute identification. if not set, consts.Default.KEY_ATTR used.
  • valuekey (str) – element tag value identification. if not set, consts.Default.KEY_VALUE used.

Changed in version 1.2: The fp is a filename of string before this. It can now be a file or file-like object that support .write() to write the xml content.