[XML-SIG] XML Parsing Newbie

Mark Tolonen metolone+gmane at gmail.com
Wed Nov 26 02:44:44 CET 2008


"Rbrennan" <brennan.ron at gmail.com> wrote in message 
news:20689034.post at talk.nabble.com...
>
> Hello,
>
> I am a xml parsing newbie and I am having a hard time because I am so new.
> 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.
>
> Here is my febal attempt at getting the values between the tags:
>
> import xml.dom.minidom
> import sys
>
> class textHandler:
>
>    def parseFile(self):
>      tags =
> xml.dom.minidom.parse("/home/grinder/grinder-3.0.1/data/reportsInfo.txt")
>      serverelements = tags.getElementsByTagName ( 'server' )
>      for server in serverelements:
>          print server.childNodes[0].data
>
> x = textHandler()
> x.parseFile()
>
>
> Can anyone help?

Under the DOM model, the first child node in <server> is a text node 
containing the whitespace between <server> and <config>.  If you know there 
is only one threads element under each server you can use:

for server in serverelements:
    print server.getElementsByTagName('threads')[0].childNodes[0].data

But 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

-Mark




More information about the XML-SIG mailing list