dhp.xml package

Module contents

routines generally helpful for dealing with icky xml

exception dhp.xml.MissingRequiredException

Bases: exceptions.Exception

A required data element is not present

exception dhp.xml.NoRootExeception

Bases: exceptions.Exception

raised when dictionary to build xml document from does not have a single ‘root’ node elment. i.e. {'root':{ ... }}

dhp.xml.dict_to_xml(dictionary, attrs=None)

return a string representation of an xml document of the dictionary. ( with optional attributes for the root node.)

>>> the_dict = {'root': {'foo': '1'}}
>>> dict_to_xml(the_dict)
<?xml version="1.0" ?><root xml:lang="en-US"><foo>1</foo></root>

Since the function returns a full xml document, the dictionary has to closely approximate the structure of the xml document. So the top level of the dictionary must be a string key with a dictionary for a value.

Also, ALL leaf node element values must be strings.

Parameters:
  • dictionary (dict) – an approximation of the xml document desired as a Python dictionary.
  • args (dict) – a dicitionary containing attributes to assign to the root level node.
Raises:

NoRootExeception – When there top level dictionary has more than 1 key/value or if the value of the top level key is not a dictionary.

Returns:

a string representing an xml document based on the inputs.

Return type:

str

dhp.xml.obj_to_xml(obj)

serialize an object’s non-private/non-hidden data attributes

dhp.xml.ppxml(xmls, indent=u' ')

pretty print xml, stripping an existing formatting

>>> buf = '<root><foo>1</foo></root>'
>>> ppxml(buf)
<?xml version="1.0" ?>
<root>
  <foo>1</foo>
</root>
Parameters:
  • xmls (str) – an xml string, either a fragment or document
  • indent (str) – a string containing the white space to use for indentation.
Returns:

A transform of that string with new lines and standardized

indentation. Default is 2 spaces indent='  '

Return type:

str

dhp.xml.xml_to_dict(xml_buf)

convert xml string to a dictionary, not always pretty, but reliable