Skip to main content

large output in XML using unicode and namespaces

Project description

loxun is a Python module to write large output in XML using Unicode and namespaces. Of course you can also use it for small XML output with plain 8 bit strings and no namespaces.

Loxun’s features are:

  • low memory foot print: the document is created on the fly by writing to an output stream, no need to keep all of it in memory

  • easy to use namespaces: simply add a namespace and refer to it using the standard namespace:element syntax.

  • mix unicode and string: pass both unicode or plain 8 bit strings to any of the methods. Internally loxun converts them to unicode, so once a parameter got accepted by the API you can rely on it not resulting in any messy UnicodeError trouble.

  • automatic escaping: no need to manually handle special characters such as < or & when writing text and attribute values.

  • robustness: while you write the document, sanity checks are performed on everything you do. Many silly mistakes immediately result in an XmlError, for example missing end elements or references to undeclared namespaces.

  • open source: distributed under the GNU Lesser General Public License 3 or later.

Writing a simple document

The following example creates a very simple XHTML document.

To make it simple, the output goes to a string, but you could also use a file that has been created using open().

>>> from StringIO import StringIO
>>> out = StringIO()

First create an XmlWriter to write the XML code to the specified output:

>>> xml = XmlWriter(out)

Then add the document prolog:

>>> xml.prolog()
>>> print out.getvalue().rstrip("\r\n")
<xml version="1.0" encoding="utf-8">

Next add the <html> start element:

>>> xml.startElement("html")

Now comes the <body>. To pass attributes, specify them in a dictionary. So in order to add:

<body id="top">

use:

>>> xml.startElement("body", {u"id":u"top"})

Let’ add a little text so there is something to look at:

>>> xml.text("Hello world!")
>>> xml.newline()

Wrap it up: close all elements and the document.

>>> xml.endElement()
>>> xml.endElement()
>>> xml.close()

And this is what we get:

>>> print out.getvalue().rstrip("\r\n")
<xml version="1.0" encoding="utf-8">
<html>
  <body id="top">
Hello world!
  </body>
</html>

Now the same thing but with a namespace. First create the prolog and header like above:

>>> out = StringIO()
>>> xml = XmlWriter(out)
>>> xml.prolog()

Next add the namespace:

>>> xml.addNamespace("xhtml", "http://www.w3.org/1999/xhtml")

Now elements can use qualified element names using a colon (:) to separate namespace and element name:

>>> xml.startElement("xhtml:html")
>>> xml.startElement("xhtml:body")
>>> xml.text("Hello world!")
>>> xml.newline()
>>> xml.endElement()
>>> xml.endElement()
>>> xml.close()

As a result, element names are now prefixed with “xhtml:”:

>>> print out.getvalue().rstrip("\r\n")
<xml version="1.0" encoding="utf-8">
<xhtml:html xlmns:xhtml="http://www.w3.org/1999/xhtml">
  <xhtml:body>
Hello world!
  </xhtml:body>
</xhtml:html>

Version history

Version 0.1, 15-May-2010

  • Initial release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

loxun-0.1.zip (8.3 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page