SAX parse string to Handler??

Discussion in 'Python' started by Juliano Freitas, Nov 8, 2004.

  1. 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
    Juliano Freitas, Nov 8, 2004
    #1
    1. Advertising

  2. Juliano Freitas

    Alan Kennedy Guest

    [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,

    --
    alan kennedy
    ------------------------------------------------------
    email alan: http://xhaus.com/contact/alan
    Alan Kennedy, Nov 8, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SL
    Replies:
    1
    Views:
    580
    Martin Honnen
    Dec 21, 2004
  2. Juliano Freitas

    Sax Handler

    Juliano Freitas, Nov 6, 2004, in forum: Python
    Replies:
    1
    Views:
    341
    Mike Meyer
    Nov 6, 2004
  3. Replies:
    4
    Views:
    4,338
  4. Replies:
    19
    Views:
    1,102
    Daniel Vallstrom
    Mar 15, 2005
  5. Kelie
    Replies:
    2
    Views:
    283
    Kelie
    Nov 30, 2007
Loading...

Share This Page