[XML-SIG] [ pyxml-Bugs-658932 ] validate and handle content at the same
noreply@sourceforge.net
noreply@sourceforge.net
Fri, 27 Dec 2002 01:26:39 -0800
Bugs item #658932, was opened at 2002-12-27 10:26
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=106473&aid=658932&group_id=6473
Category: xmlproc
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Remy C. Cool (remycool)
Assigned to: Lars Marius Garshol (larsga)
Summary: validate and handle content at the same
Initial Comment:
Debian GNU/Lunux testing
Python version 2.1
When parser.setFeature(feature_validation, 1) is set ...
the DTD is
checked against the XML file but closing tags are not
being printed.
When parser.setFeature(feature_validation, 0) is set or
the line is
commented out, closing tags are printed but the xml file is
not
validated against the DTD. How can I get both ... a
working
endElement handler and DTD validation?
-- example --
#!/usr/bin/python
#
# XML validator
#
import sys
from xml.sax import make_parser
from xml.sax.handler import feature_namespaces,
feature_validation
from xml.sax.handler import ContentHandler,
ErrorHandler, DTDHandler
class validator(ContentHandler):
def __init__(self):
# initialize
self.warnings = []
self.errors = []
def endElement(self, name):
print name
def warning(self, exception):
self.warnings.append(exception)
def error(self, exception):
self.errors.append(exception)
def fatalError(self, exception):
self.errors.append(exception)
if __name__ == '__main__':
# get arguments
arguments = sys.argv
if len(arguments) > 1:
xml_file = arguments[1]
# create a parser
parser = make_parser('xml.sax.drivers2.drv_xmlproc')
# tell parser we are not interested in XML
namespaces
parser.setFeature(feature_namespaces, 0)
# tell parser to enable validation
parser.setFeature(feature_validation, 1)
# create the handler
valxml = validator()
# tell the parser to use our handler
parser.setContentHandler(valxml)
parser.setErrorHandler(valxml)
parser.setDTDHandler(valxml)
# Parse the input
parser.parse(xml_file)
# create report
print '='*30
print 'RESULTS: %s' % xml_file
print '='*30
print 'Warnings:', len(valxml.warnings)
print 'Errors :', len(valxml.errors)
print
if valxml.warnings:
for warning in valxml.warnings:
print 'Warning: %s' % str(warning)
if valxml.errors:
for error in valxml.errors:
print 'Error : %s' % str(error)
else:
# no xml file given
print 'Usage: xml_validate.py [xml-file]'
Kind regards,
Remy Cool
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=106473&aid=658932&group_id=6473