Skip to content Skip to sidebar Skip to footer

Generate Xml With Sax2 In Python

I have a data model or an object from a class, and I need to initialize it by reading from an xml file, or create this object from scratch and output it to an xml file. Previously,

Solution 1:

Try the pythonic XML processing way: ElementTree.

Generating XML output is easy with`xml.etree.ElementTree.ElementTree.write().

write(file, encoding="us-ascii", xml_declaration=None, method="xml")

Writes the element tree to a file, as XML. file is a file name, or a file object opened for writing. encoding 1 is the output encoding (default is US-ASCII). xml_declaration controls if an XML declaration should be added to the file. Use False for never, True for always, None for only if not US-ASCII or UTF-8 (default is None). method is either "xml", "html" or "text" (default is "xml"). Returns an encoded string.

Example loading ElementTree object from text file:

>>>from xml.etree.ElementTree import ElementTree>>>tree = ElementTree()>>>tree.parse("index.xhtml")

Post a Comment for "Generate Xml With Sax2 In Python"