[XML-SIG] XML Parsing Newbie

Stefan Behnel stefan_ml at behnel.de
Wed Nov 26 10:23:54 CET 2008


Mark Tolonen wrote:
> "Rbrennan" wrote
>> I am trying to parse:
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <server>
>> <config>
>> <threads>100</threads>
>> <runs>1</runs>
>> <duration>200</duration>
>> <process>400</process>
>> <rampup>500</rampup>
>> </config>
>> </server>
>>
>> I want to grab the value of threads which is 100, runs which is 1,
>> duration which is 200, process which is 400, and rampup which is 500.
> 
> you will probably find ElementTree a much more useful xml library:
> 
> from xml.etree import ElementTree
> tree =
> ElementTree.parse("/home/grinder/grinder-3.0.1/data/reportsInfo.txt")
> print tree.find('config/threads').text

There's also lxml.objectify, which seems well adapted to your data.

    >>> from lxml import objectify
    >>> server = objectify.parse('thefile.xml').getroot()
    >>> server.config.threads
    100

Note how it shows 100, not '100'.

http://codespeak.net/lxml/

Stefan



More information about the XML-SIG mailing list