XmlBuilder

XML generator.

Requires: ElementTree

source, coloured source


    >>> from xmlbuilder import XmlFragment
    >>> xml = XmlFragment()
    >>> root = xml.div()
    >>> print root
    <_XmlElement div>
    >>> print xml
    <div />
    >>> firstchild = root.p("Some text", id="1")
    >>> print xml
    <div><p id="1">Some text</p></div>
    >>> firstchild.br()
    <_XmlElement br>
    >>> print xml
    <div><p id="1">Some text<br /></p></div>
    >>> firstchild.add_text("more text, ")
    <_XmlText>
    >>> print xml
    <div><p id="1">Some text<br />more text, </p></div>
    >>> firstchild.add_text("and more.").br()
    <_XmlElement br>
    >>> print xml
    <div><p id="1">Some text<br />more text, and more.<br /></p></div>
    >>> root.add_comment("COMMENT")
    <_XmlComment>
    >>> print xml
    <div><p id="1">Some text<br />more text, and more.<br /></p><!-- COMMENT -->
    </div>
    >>> from xmlbuilder import XmlDocument
    >>> xml = XmlDocument("us-ascii")
    >>> print xml
    Traceback (most recent call last):
        ...
    MissingDocumentRootException
    >>> root = xml.items(catalog="R&D")
    >>> print xml
    <?xml version='1.0'?>
    <items catalog="R&amp;D" />
    >>> root.item("Item 1", id="1")
    <_XmlElement item>
    >>> root.item("Item 2", id="2")
    <_XmlElement item>
    >>> print xml
    <?xml version='1.0'?>
    <items catalog="R&amp;D"><item id="1">Item 1</item><item id="2">Item 2</item></items>