[XML-SIG] problem with parser.setFeature(feature_validation, 1)

Remy C. Cool dev-xml@smartology.nl
Tue, 17 Dec 2002 19:22:05 +0100

Working example:

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?


// CODE //

# 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):

   def error(self, exception):

   def fatalError(self, 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
      # Parse the input
      # create report
      print '='*30
      print 'RESULTS: %s' % xml_file
      print '='*30
      print 'Warnings:', len(valxml.warnings)
      print 'Errors  :', len(valxml.errors)

      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)
      # no xml file given
      print 'Usage: xml_validate.py [xml-file]'