Re: Parsing Help

"Gabriel Genellina" <[email protected]>
Newsgroups gmane.comp.python.general,gmane.comp.python.xml
Message-ID <[email protected]>
En Tue, 10 Jul 2007 11:32:48 -0300, Robert Rawlins - Think Blue  
<[email protected]> escribió:

> 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.

> Now, the piece of information I'm looking to retrieve is inside the
> <attribute id="0x0004"> element and is, in this example <uint8  
> value="0x05"
> />, however I want the function to return the standard integer value and  
> not
> the unit8 encoded version, so instead of my function returning '0x05' it
> just needs to return '5' which is the standard integer version.

Try this:

def myFunction(xmlAsString):
     doc = ET.fromstring(xmlAsString)
     for att in doc.findall("attribute"):
         if att.get("id")=="0x0004":
             # obtain the second grandchildren whose tag="sequence" (begin  
its parent "sequence" too)
             seq = att.findall("sequence/sequence")[1]
             value = seq.find("uint8").get("value")
             if value[:2]=="0x":
                 return int(value, 16)
             else:
                 return int(value)

Using lxml <http://codespeak.net/lxml/> you could use XPath notation to  
simplify the navigation a little.

-- 
Gabriel Genellina

-- 
http://mail.python.org/mailman/listinfo/python-list
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.