setDocumentLocator in validating parser (xmlproc)

Discussion in 'Python' started by Cees Wesseling, Mar 18, 2005.

  1. Hi,

    it seems that xmlproc, the default Validating parser, in my setup does
    not call back to setDocumentLocator. 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.sax2exts

    class BaseHandler(ContentHandler):
    def setDocumentLocator(self,locator):
    print "setDocumentLocator called"
    self.d_locator=locator

    def startElement(self, name, attr):
    print "startElement", name


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

    vp = xml.sax.sax2exts.XMLValParserFactory.make_parser()
    print "vp type", vp.__class__
    vph = BaseHandler()
    vp.setContentHandler(vph)
    vp.parse("e.xml")

    np = xml.sax.make_parser()
    print "np type", np.__class__
    nph = BaseHandler()
    np.setContentHandler(nph)
    np.parse("e.xml")

    OUTPUT:
    vp type xml.sax.drivers2.drv_xmlproc.XmlprocDriver
    startElement E
    np type xml.sax.expatreader.ExpatParser
    setDocumentLocator called
    startElement E
     
    Cees Wesseling, Mar 18, 2005
    #1
    1. Advertising

  2. Cees Wesseling

    James Kew Guest

    "Cees Wesseling" <> wrote in message
    news:...
    > it seems that xmlproc, the default Validating parser, in my setup does
    > not call back to setDocumentLocator. Is there anyway to get a locator
    > in my handler?


    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=detail&aid=835638&group_id=6473&atid=106473

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

    import xml.sax.drivers2.drv_xmlproc

    # Override the set_locator method.
    def set_locator(self, locator):
    # Existing code.
    self._locator = locator
    # ...but also call the ContentHandler.
    # drv_xmlproc already implements the Locator interface.
    self._cont_handler.setDocumentLocator(self)

    setattr(xml.sax.drivers2.drv_xmlproc.XmlprocDriver, "set_locator",
    set_locator)

    HTH,

    James Kew
    http://jameskew.blogspot.com
     
    James Kew, Mar 18, 2005
    #2
    1. Advertising

  3. "James Kew" <> wrote in message news:<d1fml1$fn6$>...

    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.And both one columnNumber off. A bit
    annoying when swapping parsers.

    Cees
     
    Cees Wesseling, Mar 19, 2005
    #3
    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. Helen
    Replies:
    0
    Views:
    840
    Helen
    Mar 2, 2004
  2. Ellinghaus, Lance

    xmlproc returning all strings as unicode?

    Ellinghaus, Lance, Oct 10, 2003, in forum: Python
    Replies:
    1
    Views:
    280
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Oct 10, 2003
  3. Alban Hertroys

    xmlproc maintainer?

    Alban Hertroys, Mar 18, 2005, in forum: Python
    Replies:
    1
    Views:
    335
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Mar 18, 2005
  4. Tim Arnold

    docbook and xmlproc

    Tim Arnold, Jan 22, 2008, in forum: Python
    Replies:
    1
    Views:
    345
    Stefan Behnel
    Jan 23, 2008
  5. mmm

    DTD validation and xmlproc

    mmm, Apr 23, 2008, in forum: Python
    Replies:
    2
    Views:
    528
Loading...

Share This Page