[XML-SIG] location handling in pyexpat
Speedy
jerome.chantelauze@finix.eu.org
Thu, 9 Nov 2000 11:44:41 +0100
--f2QGlHpHGjS2mn6Y
Content-Type: text/plain; charset=us-ascii
Hi.
I use pyexpat to parse xml stuff and I needed location, even in well
formed documents. I've made a few changes in pyexpat to make it work,
but I'm not sure it's the "right" way to do it.
I just added a few lines in the file drv_pyexpat.py.
I join to this e-mail
- a patch for drv_pyexpat.py (drv_pyexpat.py.patch).
- an example of code with location handling (Example.py).
This example is a (useless) python script.
$ Example.py Element_Name XML_File
It uses pyexpat to scan the file XML_File and display the position
of the elements named Element_Name.
Do you think these changes can become part of the next version of
pyxml, or do I need to rethink about it ?
Jerome Chantelauze
jerome@free-system.com
--f2QGlHpHGjS2mn6Y
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="drv_pyexpat.py.patch"
*** drv_pyexpat.py.orig Fri Nov 3 16:20:52 2000
--- drv_pyexpat.py Fri Nov 3 16:34:39 2000
***************
*** 41,53 ****
--- 41,61 ----
at[attrs[i]] = attrs[i+1]
+ # Line added for location handling
+ self.doc_handler.setDocumentLocator(self)
self.doc_handler.startElement(name,saxutils.AttributeMap(at))
def endElement(self,name):
+ # Line added for location handling
+ self.doc_handler.setDocumentLocator(self)
self.doc_handler.endElement(name)
def characters(self,data):
+ # Line added for location handling
+ self.doc_handler.setDocumentLocator(self)
self.doc_handler.characters(data,0,len(data))
def processingInstruction(self,target,data):
+ # Line added for location handling
+ self.doc_handler.setDocumentLocator(self)
self.doc_handler.processingInstruction(target,data)
--f2QGlHpHGjS2mn6Y
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="Example.py"
#!/usr/bin/python
from xml.sax import saxlib, saxexts
import sys
class my_document_handler(saxlib.DocumentHandler):
def __init__(self, Target):
self.Target=Target
self.Found=0
# Called by drv_pyexpat with a locator.
def setDocumentLocator(self, Locator):
self.Locator=Locator
def endDocument(self):
if not self.Found:
print "Element %s not found." % (self.Target,)
def startElement(self, name, attrs):
if name==self.Target:
self.Found=self.Found+1
print "Found %s element at (%s,%s)" % \
(self.Target, self.Locator.getColumnNumber(),
self.Locator.getLineNumber())
File=None
if len(sys.argv)!=3:
print "Usage: Example.py Target File"
else:
File=open(sys.argv[2], "r")
my_parser=saxexts.make_parser()
my_handler=my_document_handler(sys.argv[1])
my_parser.setDocumentHandler(my_handler)
my_parser.parseFile(File)
--f2QGlHpHGjS2mn6Y--