Python and XML Help

Scott David Daniels Scott.Daniels at Acm.Org
Mon Apr 13 20:48:33 CEST 2009


ookrin wrote:
> I'm in the process of learning python and PyQt4. I had decided to make
> myself a simple app and soon discovered that I needed to crash into
> xml to use some of the data I was going to be getting off of the
> server.
> 
> I picked up enough xml to use the sax parser to get the data out of
> the xml. I can get as far as printing it to the screen, but there is
> where I get stuck....

Assuming you are using a somewhat current Python (2.5 or later, you
did not specify), the etree package is buit in.  If not, you can
download it for your python easily.  You can then (for example)
use something like:
     from xml.etree import cElementTree as ET
     ...
     parsed = ET.parse('CharacterID.xml')
     for node in parsed.getiterator('osArchitecture'): print node.text

or if you have the data in a string,
     root = ET.fromstring('<the><xml>string</xml></the>')
     print root[0].tag, root[0].text

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list