Parsing xml file in python

A

amjadcsu

I am a newbie in python
I am trying to parse a xml file and write its content in a txt file.
The txt contains null elements. Any reason what iam doing wrong here


Here is the code that i wrote

import sys,os
import xml.sax
import xml.sax.handler
from xml.sax.handler import ContentHandler
from xml.sax import make_parser

class gmondxmlparse (ContentHandler):

def __init__(self,searchTerm):
self.searchTerm=searchTerm;

def startElement(self,name,attrs):

if name=="HOST":
self.hostname=attrs.get('NAME',"")
self.IP=attrs.get('IP',"")
elif name=="METRIC":
self.metricname=attrs.get('NAME', "")
self.metricvalue=attrs.get('VAL',"")
self.metrictype=attrs.get('TYPE',"")
self.metricunit=attrs.get('UNITS',"")
return

def endElement(self,name):
if name=="HOST" and self.searchTerm==self.hostname:
try:
fh=open('/root/yhpc-2.0/ganglia.txt' ,'w')
except:
print "File /root/yhpc-2.0/ganglia.txt can not be
open"
sys.exit(1)
fh.write("This is a test for xml parsing with python with
chris and amjad \n")
fh.write("the host name is", self.hostname, "\n")
fh.write("the ip address is", self.IP, "\n")
fh.close()

searchTerm="HOST"
parser=make_parser()
curHandler=gmondxmlparse(searchTerm)
parser.setContentHandler(curHandler)
parser.parse(open("/root/yhpc-2.0/gmond.xml"))


Here is the sample of xml file

Here is the xmk file called gmond.xml
<HOST NAME="192.168.10.163" IP="192.168.10.163" REPORTED="1193689455"
TN="0" TMAX="20" DMAX="0" LOCATION="unspecified"
GMOND_STARTED="1193170061">
<METRIC NAME="cpu_num" VAL="2" TYPE="uint16" UNITS="CPUs" TN="994"
TMAX="1200" DMAX="0" SLOPE="zero" SOURCE="gmond"/>
 
D

Diez B. Roggisch

I am a newbie in python
I am trying to parse a xml file and write its content in a txt file.
The txt contains null elements. Any reason what iam doing wrong here


Here is the code that i wrote

import sys,os
import xml.sax
import xml.sax.handler
from xml.sax.handler import ContentHandler
from xml.sax import make_parser

class gmondxmlparse (ContentHandler):

def __init__(self,searchTerm):
self.searchTerm=searchTerm;

def startElement(self,name,attrs):

if name=="HOST":
self.hostname=attrs.get('NAME',"")
self.IP=attrs.get('IP',"")
elif name=="METRIC":
self.metricname=attrs.get('NAME', "")
self.metricvalue=attrs.get('VAL',"")
self.metrictype=attrs.get('TYPE',"")
self.metricunit=attrs.get('UNITS',"")
return

def endElement(self,name):
if name=="HOST" and self.searchTerm==self.hostname:
try:
fh=open('/root/yhpc-2.0/ganglia.txt' ,'w')
except:
print "File /root/yhpc-2.0/ganglia.txt can not be
open"
sys.exit(1)
fh.write("This is a test for xml parsing with python with
chris and amjad \n")
fh.write("the host name is", self.hostname, "\n")
fh.write("the ip address is", self.IP, "\n")
fh.close()

searchTerm="HOST"
parser=make_parser()
curHandler=gmondxmlparse(searchTerm)
parser.setContentHandler(curHandler)
parser.parse(open("/root/yhpc-2.0/gmond.xml"))


Here is the sample of xml file

Here is the xmk file called gmond.xml
<HOST NAME="192.168.10.163" IP="192.168.10.163" REPORTED="1193689455"
TN="0" TMAX="20" DMAX="0" LOCATION="unspecified"
GMOND_STARTED="1193170061">
<METRIC NAME="cpu_num" VAL="2" TYPE="uint16" UNITS="CPUs" TN="994"
TMAX="1200" DMAX="0" SLOPE="zero" SOURCE="gmond"/>

Without an actual error given, it's hard to know what your problem is.

One thing though is noticable: your XML below isn't valid - XML has only
one root-element.

And just for the record: it appears that you work under linux using a
root-account. Bad idea. Really.

http://linuxbraindump.org/2007/08/13/the-10-commandments-for-new-linux-users/

Diez
 
A

amjadcsu

That XML is just a snapshot
I am not getting into the xml parser. The error is not generated but
also the /root/yhpc-2.0/ganglia.txt does not contain anything.
 
M

Marc 'BlackJack' Rintsch

I am not getting into the xml parser.

What does this mean!?
The error is not generated but also the /root/yhpc-2.0/ganglia.txt does
not contain anything.

Maybe because…

…this line will raise an exception. `file.write()` takes just one
argument, not three as in this call. If you don't get an exception maybe
you have other places with a bare ``except`` like in the snippet above.
Don't do that. Catch the specific exception you want to handle with an
``except`` and not simply *all*.

Ciao,
Marc 'BlackJack' Rintsch
 
J

J. Clifford Dyer

On Tue, Oct 30, 2007 at 11:45:17AM -0700, (e-mail address removed) wrote regarding Re: Parsing xml file in python:

Top-posting corrected....
That XML is just a snapshot
I am not getting into the xml parser. The error is not generated but
also the /root/yhpc-2.0/ganglia.txt does not contain anything.

Well, if ganglia.txt contains nothing, and you received no output from the program, then either endElement never got called, or `if name=="HOST" and self.searchTerm==self.hostname:` never evaluated to true. Because if you couldn't open for writing, you would have gotten the message you set up on the except block, and if you could, then even if your variables didn't contain any data, you would have seen the boilerplate text that you wrote.

Cheers,
Cliff

P.S. Please bottom-post when replying to the python list. It sucks to have to look up and down a thread to see what's been said.
 
S

Stefan Behnel

I am a newbie in python
I am trying to parse a xml file and write its content in a txt file.

If you want to write code that does not hide your bugs behind cryptic event
handlers and instead helps you get XML work done, try using ElementTree or
lxml instead of SAX. The first is in the standard library of Python 2.5, the
second is here:

http://codespeak.net/lxml

Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top