Limited XML tidy

Discussion in 'Python' started by Toby White, Aug 23, 2005.

  1. Toby White

    Toby White Guest

    I have a program which produces well-formed XML documents,
    but takes several hours if not days to do so. It would
    be useful to be able to take the incomplete output and
    manipulate it as XML.

    Clearly, however, the incomplete output will not be well-
    formed, so before being able to manipulate it I need to
    make it wellformed.

    This is essentially fairly simple. I know it's well-formed
    up to now - all I need to do is close unclosed tags. So,
    I 've made a short function that will do this:

    #!/usr/bin/python

    import sys
    import xml.sax

    tagStack = []
    closingTags = ""

    class DodgyHandler(xml.sax.ContentHandler):
    def startElement(self, tag, attributes):
    tagStack.append(tag)

    def endElement(self, tag):
    tagStack.pop()


    class DodgyErrorHandler(xml.sax.ErrorHandler):
    def fatalError(self,exception):
    global closingTags
    tagStack.reverse()
    for tag in tagStack:
    closingTags += "</%s>" % tag
    return closingTags

    filename = sys.argv[1]

    p = xml.sax.make_parser()
    p.setContentHandler(DodgyHandler())
    p.setErrorHandler(DodgyErrorHandler())

    f = open(filename, mode='r')

    for line in f:
    print line,
    p.feed(line)
    p.close()

    print closingTags

    However - while this works for 90% of the cases I need,
    it fails in the case where my incomplete output stops in the
    middle of a tag (not to mention some other more arcane
    places I don't really care about).

    The problem is that when the sax handler raises an exception,
    I can't see how to find out why. What I want to do is for
    DodgyErrorHandler to do something different depending on
    where we are in the course of parsing. Is there anyway
    to get that information back from xml.sax (or indeed from
    any other sax handler?)

    Toby

    --
    Dr. Toby White
    Dept. of Earth Sciences, Downing Street, Cambridge CB2 3EQ. UK
    Email: <>
     
    Toby White, Aug 23, 2005
    #1
    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. Eric
    Replies:
    0
    Views:
    537
  2. Toby White

    Limited XML tidy

    Toby White, Aug 23, 2005, in forum: Python
    Replies:
    3
    Views:
    396
    Toby White
    Aug 26, 2005
  3. Replies:
    6
    Views:
    381
    Magnus Lycka
    Feb 27, 2006
  4. Ragnar
    Replies:
    9
    Views:
    1,527
    Andy Dingley
    Oct 27, 2006
  5. Ragnar
    Replies:
    13
    Views:
    600
    Joseph Kesselman
    Nov 8, 2006
Loading...

Share This Page