Re: Parsing Help

Dave Kuhlman <[email protected]>
Newsgroups gmane.comp.python.xml
Message-ID <[email protected]>
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

Or:

    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


-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
_______________________________________________
XML-SIG maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/xml-sig
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.