Tools to write XML files, without having to deal with encoding issues, well-formedness, etc.
The current version does not provide built-in support for namespaces. To create files using namespaces, you have to provide "xmlns" attributes and explicitly add prefixes to tags and attributes.
from elementtree.SimpleXMLWriter import XMLWriter import sys w = XMLWriter(sys.stdout) html = w.start("html") w.start("head") w.element("title", "my document") w.element("meta", name="generator", value="my application 1.0") w.end() w.start("body") w.element("h1", "this is a heading") w.element("p", "this is a paragraph") w.start("p") w.data("this is ") w.element("b", "bold") w.data(" and ") w.element("i", "italic") w.data(".") w.end("p") w.close(html)
XML writer class.
For more information about this class, see The XMLWriter Class.
XML writer class.
Closes open elements, up to (and including) the element identified by the given identifier.
Adds a comment to the output stream.
Adds character data to the output stream.
Writes an XML declaration.
Adds an entire element. This is the same as calling start, data, and end in sequence. The text argument can be omitted.
Closes the current element (opened by the most recent call to start).
Flushes the output stream.
Opens a new element. Attributes can be given as keyword arguments, or as a string/string dictionary. You can pass in 8-bit strings or Unicode strings; the former are assumed to use the encoding passed to the constructor. The method returns an opaque identifier that can be passed to the close method, to close all open elements up to and including this one.