Skip to content
bbolli edited this page Sep 9, 2011 · 4 revisions

xmltramp: Make XML documents easily accessible

xmltramp is a Python library based on xml.sax that simplifies handling XML documents.

Loading XML data into a document

from a string

doc = xmltramp.parse("<foo version='1'><bar name='foobar'/></foo>")

from a file

doc = xmltramp.seed(open("test.xml"))

from an URL

doc = xmltramp.load("http://rss.slashdot.org/Slashdot/slashdot")

Accessing the XML data

Each node is represented by a xmltramp.Element instance.

A node's text content can be read with unicode(elt), which returns a Unicode string (duh!), or with str(elt), which returns a plain ASCII string with XML character references where needed.

A node's attributes can be read with the call syntax elt(attrname), so doc('version') returns '1'. elt() without parameters returns a dictionary of all attributes and their values.

Child nodes are read with array subscript syntax.

  • node['subtag'] returns the first <subtag> child of node, so doc['bar'] returns the <bar> node.
  • To iterate over all child nodes with a given tag, use a slice: node[tagname:] returns a list of all 'tagname' children of node.
  • To iterate over all child nodes regardless of their tag, use the empty slice: node[:] returns a list of all children of node.
  • A numeric index n returns the n-th child node.
  • Numeric slices return the expected subset of child nodes.

Producing XML

Start by creating the root node:

root = Element('foo')

or by parsing a template XML document.

The Element constructor takes these keyword parameters:

  • attrs, a dict of node attributes
  • children, a list of child nodes (themselves Elements or strings)
  • value:
    • a string that becomes the node's text content
    • a list of attribute name, value pairs (not tuples!)
    • a dict of attributes

A node's children can be changed using array subscript syntax again:

  • node["bar"] = x replaces the first bar child with x and deletes all other barchildren
  • node["bar":] = x appends a new bar node with content x
  • node[0] = x replaces the first subnode with x
  • node[None] = x appends x to node's children. x may be a string or an Element instance.

x is interpreted like the value constructor parameter.

A node's attributed can be changed with call syntax using positional and/or keyword arguments:

  • node('attrname', 'value') sets one attrname to value
  • node('a1', 'v1', 'a2', 'v2') sets two attributes, etc.
  • node(attr=value) sets one attribute

If the same attribute is set using both positional and keyword arguments, the positional value wins.

The final XML string can be generated from a node with unicode(node) or str(node).