Producing XML Output¶
You can create XML output using the
emit_xml method of one of the XML
node classes to produce XML output from the node’s data.
You can also use the package’s emit_xml() function to directly
convert a Python object to XML output.
Producing XML Output from an XML Node Object¶
You can produce XML output from an XMLDictNode,
XMLListNode, or XMLCDATNode instance. You use
the emit_xml class method to produce
the output.
-
XMLNodeBase.emit_xml(output=None, encoding='utf-8', handler=<class xml.sax.saxutils.XMLGenerator>, **kwargs)[source]¶ Return the contents of the XML tree as an XML document.
This method will create a
ContentHandlerby calling the method provided by the handler parameter. It will callemit_handler()with thisContentHandlerobject. In addition, this method will accept any parameter that theemit_handler()method accepts (except thecontent_handlerparameter). It will pass them to theemit_handler()method when it calls it.Parameters: - output (A file-like IO object, or None) – The file-like IO object in which output should be placed. If None, the method will return the XML output as a string.
- encoding (string) – The encoding that should be used for the output.
- handler (function) – A method that will return a
ContentHandlerobject. This method will be called with two positional parameters: the output parameter (or, if None, a file-like IO object) and the encoding parameter.
Returns: If
outputwas None, the method will return the XML output as a string. Otherwise, None.
-
XMLNodeBase.emit_handler(content_handler, pretty=True, newl='\n', indent=' ', full_document=None)[source]¶ Pass the contents of the XML tree to a ContentHandler object.
This method will pass the contents of the XML tree to a
ContentHandlerobject.Parameters: - content_handler (
ContentHandler) – TheContentHandlerobject to which the XML tree wll be passed. - pretty (bool) – If True, this method will call the
content_handler.ignorableWhitespace()method to add - to the output document. (whitespace) –
- newl (string) – The string which the method should use for new
lines when adding white space (see the
prettyparameter). - indent (text) – The string which the method should use for each
level of indentation when adding white space (see the
prettyparameter). - full_document (bool) – If True, the method will call the
content_handler.startDocument()andcontent_handler.endDocument()methods at the start and end of the document, respectively. If False, it will not call these methods. If the parameter is not set, the method will attempt to determine whether the current node is the root of an XML tree with a single root tag. If so, it will set the full_document parameter to True; otherwise, it will set it to False.
Returns: None
- content_handler (
Producing XML Output from a Python Object¶
You can produce XML output from a normal Python dictionary or list
using the emit_xml() function.
-
jxmlease.emit_xml(obj, *args, **kwargs)[source]¶ Translate a Python dictionary or list to XML output.
This method internally creates an
XMLDictNodeorXMLListNodeobject, as appropriate, and then calls theemit_xmlmethod of that class.Any arguments you supply are passed on to the
emit_xmlmethod of that class. Please see the documentation for the class’emit_xmlmethod for information on arguments and return values.Raises: TypeError– If the object is not an appropriate type to convert to an XML tree.