poppy.core.tools.xmltodict.xmltodict module

Makes working with XML feel like you are working with JSON

exception poppy.core.tools.xmltodict.xmltodict.ParsingInterrupted[source]

Bases: Exception

poppy.core.tools.xmltodict.xmltodict.parse(xml_input, encoding=None, expat=<module 'xml.parsers.expat' from '/home/docs/.pyenv/versions/3.6.8/lib/python3.6/xml/parsers/expat.py'>, process_namespaces=False, namespace_separator=':', **kwargs)[source]

Parse the given XML input and convert it into a dictionary.

xml_input can either be a string or a file-like object.

If xml_attribs is True, element attributes are put in the dictionary among regular child elements, using @ as a prefix to avoid collisions. If set to False, they are just ignored.

Simple example:

>>> import xmltodict
>>> doc = xmltodict.parse("""
... <a prop="x">
...   <b>1</b>
...   <b>2</b>
... </a>
... """)
>>> doc['a']['@prop']
u'x'
>>> doc['a']['b']
[u'1', u'2']

If item_depth is 0, the function returns a dictionary for the root element (default behavior). Otherwise, it calls item_callback every time an item at the specified depth is found and returns None in the end (streaming mode).

The callback function receives two parameters: the path from the document root to the item (name-attribs pairs), and the item (dict). If the callback’s return value is false-ish, parsing will be stopped with the ParsingInterrupted exception.

Streaming example:

>>> def handle(path, item):
...     print 'path:%s item:%s' % (path, item)
...     return True
...
>>> xmltodict.parse("""
... <a prop="x">
...   <b>1</b>
...   <b>2</b>
... </a>""", item_depth=2, item_callback=handle)
path:[(u'a', {u'prop': u'x'}), (u'b', None)] item:1
path:[(u'a', {u'prop': u'x'}), (u'b', None)] item:2

The optional argument postprocessor is a function that takes path, key and value as positional arguments and returns a new (key, value) pair where both key and value may have changed. Usage example:

>>> def postprocessor(path, key, value):
...     try:
...         return key + ':int', int(value)
...     except (ValueError, TypeError):
...         return key, value
>>> xmltodict.parse('<a><b>1</b><b>2</b><b>x</b></a>',
...                 postprocessor=postprocessor)
OrderedDict([(u'a', OrderedDict([(u'b:int', [1, 2]), (u'b', u'x')]))])

You can pass an alternate version of expat (such as defusedexpat) by using the expat parameter. E.g:

>>> import defusedexpat
>>> xmltodict.parse('<a>hello</a>', expat=defusedexpat.pyexpat)
OrderedDict([(u'a', u'hello')])
poppy.core.tools.xmltodict.xmltodict.unparse(input_dict, output=None, encoding='utf-8', full_document=True, **kwargs)[source]

Emit an XML document for the given input_dict (reverse of parse).

The resulting XML document is returned as a string, but if output (a file-like object) is specified, it is written there instead.

Dictionary keys prefixed with attr_prefix (default=`’@’) are interpreted as XML node attributes, whereas keys equal to `cdata_key (default=`’#text’`) are treated as character data.

The pretty parameter (default=`False`) enables pretty-printing. In this mode, lines are terminated with `

‘` and indented with ‘ ‘, but this
can be customized with the newl and indent parameters.