on May 19th, 2008pySX: Simple XML processing in Python

pySX is my latest hobby project. It’s a very simple XML library that aims to simplify the process of handling XML as much as possible. The code can be found here: http://svn.loveandtheft.org/fredrik/pysx. Here are a couple of quick usage examples:

“Manual”

When accessing an element, two things can happen:

  • You get a list back, which means that there are more then one element with that name
  • You get an object of type pysx.node.Element back, which means there’s only one element with that name

Examples

Loading an xml hierarchy

import pysx
tree = pysx.handler.parse('users.xml')

Iterating over a collection of elements

for user in tree.root.users:
     print user.name
     print user.age
     print user.get_child('pysx:age') # Also under it's namespace

Accessing the n:th element

print tree.root.user[0].name

Accessing attributes

print tree.root.user['id']
print tree.root.user['pysx:id'] # namespaced

Replacing an element with another one

# replaces the first <user> with a <admin>
print tree.root.user[0].replace_self(pysx.node.Element(qname = 'admin'))
print tree.root.admin.name

Removing an element

tree.root.admin.remove_self()

2 Responses to “pySX: Simple XML processing in Python”

  1. aurelianon 06 Jun 2008 at 4:20 pm

    there is something wrong with your svn-repo:

    svn: PROPFIND of ‘/fredrik/pysx’: could not connect to server (http://svn.loveandtheft.org)

    I’ll really like to try it :)

  2. Fredrik Holmströmon 17 Jun 2008 at 8:39 am

    Yes, my server is currently down - it’ll be up by the end of the week again, trying to relocate it to a place where it doesn’t disturbt my nights sleep :)

Trackback URI | Comments RSS

Leave a Reply