setDocumentLocator in validating parser (xmlproc)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cees Wesseling

    #1

    setDocumentLocator in validating parser (xmlproc)

    Hi,

    it seems that xmlproc, the default Validating parser, in my setup does
    not call back to setDocumentLoca tor. Is there anyway to get a locator
    in my handler?
    Below you find an example and its output.

    Regards, Cees

    # base imports
    from xml.sax.handler import ContentHandler
    from xml.sax.handler import EntityResolver
    import xml.sax
    import xml.sax.sax2ext s

    class BaseHandler(Con tentHandler):
    def setDocumentLoca tor(self,locato r):
    print "setDocumentLoc ator called"
    self.d_locator= locator

    def startElement(se lf, name, attr):
    print "startEleme nt", name


    open('e.dtd','w ').write('<!ELE MENT E EMPTY>')
    open('e.xml','w ').write(
    """<?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE E SYSTEM "e.dtd"><E/>""")

    vp = xml.sax.sax2ext s.XMLValParserF actory.make_par ser()
    print "vp type", vp.__class__
    vph = BaseHandler()
    vp.setContentHa ndler(vph)
    vp.parse("e.xml ")

    np = xml.sax.make_pa rser()
    print "np type", np.__class__
    nph = BaseHandler()
    np.setContentHa ndler(nph)
    np.parse("e.xml ")

    OUTPUT:
    vp type xml.sax.drivers 2.drv_xmlproc.X mlprocDriver
    startElement E
    np type xml.sax.expatre ader.ExpatParse r
    setDocumentLoca tor called
    startElement E
  • James Kew

    #2
    Re: setDocumentLoca tor in validating parser (xmlproc)

    "Cees Wesseling" <cees@pcraster. nl> wrote in message
    news:6b22838c.0 503181241.705ee 9e2@posting.goo gle.com...[color=blue]
    > it seems that xmlproc, the default Validating parser, in my setup does
    > not call back to setDocumentLoca tor. Is there anyway to get a locator
    > in my handler?[/color]

    It's a known bug with a simple patch -- I don't know why it wasn't fixed in
    PyXML 0.8.4.
    http://sourceforge.net/tracker/?func...73&atid=106473

    I had the same problem a while ago; I ended up doing a monkeypatch to
    xml.sax.drivers 2.drv_xmlproc to add the missing call:

    import xml.sax.drivers 2.drv_xmlproc

    # Override the set_locator method.
    def set_locator(sel f, locator):
    # Existing code.
    self._locator = locator
    # ...but also call the ContentHandler.
    # drv_xmlproc already implements the Locator interface.
    self._cont_hand ler.setDocument Locator(self)

    setattr(xml.sax .drivers2.drv_x mlproc.XmlprocD river, "set_locato r",
    set_locator)

    HTH,

    James Kew



    Comment

    • Cees Wesseling

      #3
      Re: setDocumentLoca tor in validating parser (xmlproc)

      "James Kew" <james.kew@gmai l.com> wrote in message news:<d1fml1$fn 6$1@news.astoun d.net>...

      Thanks James, a perfect patch that works perfect for me.

      On a side note, it seems the locator is not standarized. expat gives
      positions at the "<"-char of startElement while xmlproc (PyXml) gives
      it the ">"-char of startElement.An d both one columnNumber off. A bit
      annoying when swapping parsers.

      Cees

      Comment

      Working...