SAX parse string to 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'm passing in my
swdbHandler. How cai i parse a xml string??
somebody can help me with this??

Juliano Freitas
 
A

Alan Kennedy

[Juliano Freitas]
> here is my pythn code that's handle a xml file:

Juliano,

I think you're better off using the standard sax parsing facilities than
those inside saxexts, etc.

Try this code

#==================================================
import xml.sax

class swdbHandler(xml.sax.handler.ContentHandler):
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"

xml_string="<swml><contents/></swml>"

xml.sax.parseString(xml_string, swdbHandler())
#==================================================

HTH,
 

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,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top