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
ContentHandler
by calling the method provided by the handler parameter. It will callemit_handler()
with thisContentHandler
object. In addition, this method will accept any parameter that theemit_handler()
method accepts (except thecontent_handler
parameter). 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
ContentHandler
object. 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
output
was 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
ContentHandler
object.Parameters: - content_handler (
ContentHandler
) – TheContentHandler
object 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
pretty
parameter). - indent (text) – The string which the method should use for each
level of indentation when adding white space (see the
pretty
parameter). - 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
XMLDictNode
orXMLListNode
object, as appropriate, and then calls theemit_xml
method of that class.Any arguments you supply are passed on to the
emit_xml
method of that class. Please see the documentation for the class’emit_xml
method for information on arguments and return values.Raises: TypeError
– If the object is not an appropriate type to convert to an XML tree.