Skip to content

adjunct.xmlutils

XML utilities.

XMLBuilder

An XML document builder.

It's purposely namespace ignorant: it's up to user to supply appropriate xmlns attributes as needed.

Examples:

>>> xml = XMLBuilder()
>>> with xml.within("root", xmlns="tag:talideon.com,2013:test"):
...     xml += "Before"
...     with xml.within("leaf"):
...         xml += "Within"
...     xml += "After"
...     xml.tag("leaf", "Another")
>>> print xml.as_string()
<?xml version="1.0" encoding="utf-8"?>
<root xmlns="tag:talideon.com,2013:test">Before<leaf>Within</leaf>After<leaf>Another</leaf></root>

Parameters:

Name Type Description Default
out TextIOBase | None

a file-like object to write the document to; if none is provided, a buffer is created.

None
Note

If you provide your own, the as_string() method will return an empty string as no other sensible value can be returned.

append(other)

Append the string to this document.

Parameters:

Name Type Description Default
other str

a string to write to the document

required

as_string()

If using the built-in buffer, get its current contents.

close()

If using the built-in buffer, clean it up.

tag(tag, *values, **attrs)

Generate a simple element.

Parameters:

Name Type Description Default
tag str

the tag name

required
values str

any character data to write between the start and end tag

()
attrs str

any attributes to add to the tag

{}

within(tag, **attrs)

Generate an element containing nested elements.

Parameters:

Name Type Description Default
tag str

the tag name

required
attrs str

any attributes to add to the tag

{}