Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > setDocumentLocator in validating parser (xmlproc)

Thread Tools

setDocumentLocator in validating parser (xmlproc)

Cees Wesseling
Posts: n/a

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"

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

open('e.dtd','w').write('<!ELEMENT E EMPTY>')
"""<?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()

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

vp type xml.sax.drivers2.drv_xmlproc.XmlprocDriver
startElement E
np type xml.sax.expatreader.ExpatParser
setDocumentLocator called
startElement E
Reply With Quote
James Kew
Posts: n/a
"Cees Wesseling" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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.

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.

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


James Kew

Reply With Quote
Cees Wesseling
Posts: n/a
"James Kew" <(E-Mail Removed)> wrote in message news:<d1fml1$fn6$(E-Mail Removed)>...

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.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Small, non-validating XML parser/walker/reader? XML 6 03-07-2006 04:01 PM
standalone validating XML parser for Solaris? XML 4 10-21-2005 07:46 AM
Validating SAX parser still reads whitespace/does strange things. Java 1 01-07-2005 04:32 PM
Validating SGML parser? Benjamin Niemann Python 2 08-22-2004 12:06 PM
Validating XML using the Xerces DOM parser Helen Java 0 03-02-2004 12:37 AM