[XML-SIG] Parsing Help

Dave Kuhlman dkuhlman at rexx.com
Tue Jul 10 17:53:22 CEST 2007

On Tue, Jul 10, 2007 at 03:32:48PM +0100, Robert Rawlins - Think Blue wrote:
> Hello Guys,
> I'm looking for some help building a function which can parse some XML for
> me using ElementTree. The document is of a very consistent format and I've
> copied an example of the document below.

Here are some suggestions.

Import ElementTree or Lxml:

    from xml.etree import ElementTree as etree


    from lxml import etree

Parse the string:

    root = etree.fromstring(xmlstring)

Iterate over the nodes in the tree:

    for node in root.getiterator():

Check for the "attribute" tag:

    if node.tag == 'attribute':
    # But, use something like the following if there is a namespace.
    #if node.tag == '{%s}attribute' % (node.nsmap['mynamespace'], ):

Get the "id" attribute (or None is there isn't one):

    charid = node.get('id', None)

Enough to get you started?


Dave Kuhlman

More information about the XML-SIG mailing list