[XML-SIG] SAX and DTDs

Albert Chin xml-sig at mlists.thewrittenword.com
Sun Aug 28 08:02:48 CEST 2005


On Sat, Aug 06, 2005 at 11:15:36AM -0600, Uche Ogbuji wrote:
> On Thu, 2005-08-04 at 12:39 +0300, Alin Dobre wrote:
> > I have a python script that does a simple parsing of a XML document
> > using SAX. The problem is that I cannot get to validate the XML using an
> > external DTD file.
> > 
> > ------------
> > #!/bin/env python
> > import sys
> > from xml.sax import saxlib, saxexts
> > class mySaxDH(saxlib.HandlerBase):
> >   def startDocument(self):
> >     print 'Document start'
> > handler = manSaxDH(sys.stdout)
> > parser = saxexts.make_parser()
> > parser.setDocumentHandler(handler)
> > inFile = file(sys.argv[1], 'r')
> > parser.parseFile(inFile)
> > inFile.close()
> > ------------
> > <?xml version="1.0"?>
> > <!DOCTYPE man SYSTEM "my.dtd">
> > <tag>data</tag>
> > ------------
> > 
> > For the examples shown above, I want to validate the xml stream against
> > the my.dtd file. Any idea how to do this using SAX?
> 
> Use a validating parser.  e.g.
> 
> saxexts.XMLValParserFactory.make_parser()

What if you wanted to validate against an external DTD that you wish
to load separately? The following doesn't work:
  p = saxexts.XMLValParserFactory.make_parser ()
  p.parser.dtd = load_dtd ("[DTD File]")
  p.setDocumentHandler (xmlh)
  p.feed ([XML FILE AS STRING])

-- 
albert chin (china at thewrittenword.com)


More information about the XML-SIG mailing list