Parsing Help

Robert Rawlins - Think Blue robert.rawlins at thinkbluemedia.co.uk
Tue Jul 10 10:32:48 EDT 2007


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.

 

<?xml version="1.0" encoding="UTF-8" ?>

 

<record>

        <attribute id="0x0000">

                <uint32 value="0x00010005" />

        </attribute>

 

        <attribute id="0x0001">

                <sequence>

                        <uuid value="0x1105" />

                </sequence>

        </attribute>

 

        <attribute id="0x0003">

                <uuid value="0xe005" />

        </attribute>

 

        <attribute id="0x0004">

                <sequence>

                        <sequence>

                                <uuid value="0x0100" />

                        </sequence>

                        <sequence>

                                <uuid value="0x0003" />

                                <uint8 value="0x05" />

                        </sequence>

                        <sequence>

                                <uuid value="0x0008" />

                        </sequence>

                </sequence>

        </attribute>

 

        <attribute id="0x0005">

                <sequence>

                        <uuid value="0x1002" />

                </sequence>

        </attribute>

 

        <attribute id="0x0009">

                <sequence>

                        <sequence>

                                <uuid value="0x1105" />

                                <uint16 value="0x0100" />

                        </sequence>

                </sequence>

        </attribute>

 

        <attribute id="0x0100">

                <text value="OBEX Object Push" />

        </attribute>

 

        <attribute id="0x0303">

                <sequence>

                        <uint8 value="0x01" />

                        <uint8 value="0x03" />

                        <uint8 value="0x05" />

                        <uint8 value="0x06" />

                        <uint8 value="0xff" />

                </sequence>

        </attribute>

</record>

 

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.

 

I will be passing this XML into the function as a string, so the function
will be formed something like this:

 

Def myFunction(XmlAsString):

                Pass the xml and exract my value....

                

                Return the value as an integer...

 

I'm not sure on the best method to do this, I just want something nice and
quick, lightweight and that's not resource hungry. Can anyone offer some
advice on this?

 

Thanks guys,

 

Rob

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070710/f454b2d2/attachment.html>


More information about the Python-list mailing list