Re: XML Parsing Newbie
"Mark Tolonen" <[email protected]> Tue, 25 Nov 2008 17:44:44 -0800
| Newsgroups | gmane.comp.python.xml |
|---|---|
| Message-ID | <[email protected]> |
"Rbrennan" <[email protected]> wrote in message news:[email protected]... > > 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 _______________________________________________ XML-SIG maillist - [email protected] http://mail.python.org/mailman/listinfo/xml-sig