1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
"""
A command-line interface to the xmlproc parser. It continues parsing
after even fatal errors, in order to be find more errors, since this
does not mean feeding data to the application after a fatal error
(which would be in violation of the spec).
Usage:
[xpcmd.py] urltodoc
urltodoc: URL to the document to parse. (You can use plain file names
as well.)
"""
# --- INITIALIZATION
import sys
from xml.parsers.xmlproc import xmlproc
# --- ERROR HANDLING
class MyErrorHandler(xmlproc.ErrorHandler):
def __init__(self,loc):
self.locator=loc
self.errors=0
self.warnings=0
def get_location(self):
return "%s:%d:%d" % (self.locator.get_current_sysid(),\
self.locator.get_line(),
self.locator.get_column())
def warning(self,msg):
print "WARNING ON %s:" % self.get_location()
print " "+msg
self.warnings=self.warnings+1
def fatal(self,msg):
print "ERROR ON %s: " % self.get_location()
print " "+msg
self.errors=self.errors+1
sys.exit(1)
# --- MAIN PROGRAM
p=xmlproc.XMLProcessor()
err=MyErrorHandler(p)
p.set_error_handler(err)
print "Parsing document"
p.parse_resource(sys.argv[1])
print "Parse complete, %d error(s) and %d warning(s)" % \
(err.errors,err.warnings)
|