Parsing Help

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Jul 11 03:52:53 CEST 2007


En Tue, 10 Jul 2007 11:32:48 -0300, Robert Rawlins - Think Blue  
<robert.rawlins at thinkbluemedia.co.uk> 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




More information about the Python-list mailing list