Sax Handler

  • Thread starter Juliano Freitas
  • Start date
J

Juliano Freitas

hello!!!

here is my pythn code that's handle a xml file:

=============================================================
from xml.sax import saxutils, saxlib, saxexts, parseString

class swdbHandler(saxlib.DocumentHandler):
def __init__(self):
self.flag = 0

def startElement(self, name, atts):
if name == "swml":
print "enter"

def endElement(self, name):
if name == "swml":
print "exit"

parser = saxexts.make_parser()
parser.setDocumentHandler(swdbHandler())

# here
parser.parseFile(open("exemplo.xml"))
=============================================================

i want to parse a "xml string" not a file how can i pass my swdbHandler
to this string?? with parseString? i try but doesnt work?
somebody can help me with this??

Juliano Freitas
 
M

Mike Meyer

Juliano Freitas said:
hello!!!

here is my pythn code that's handle a xml file:

=============================================================
from xml.sax import saxutils, saxlib, saxexts, parseString

class swdbHandler(saxlib.DocumentHandler):
def __init__(self):
self.flag = 0

def startElement(self, name, atts):
if name == "swml":
print "enter"

def endElement(self, name):
if name == "swml":
print "exit"

parser = saxexts.make_parser()
parser.setDocumentHandler(swdbHandler())

# here
parser.parseFile(open("exemplo.xml"))
=============================================================

i want to parse a "xml string" not a file how can i pass my swdbHandler
to this string?? with parseString? i try but doesnt work?
somebody can help me with this??

Try the StringIO module. That will give you a file-like object for a string.

<mike
 

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