[XML-SIG] SAX and DTDs
Albert Chin
xml-sig at mlists.thewrittenword.com
Sun Aug 28 08:18:11 CEST 2005
On Sun, Aug 28, 2005 at 01:02:48AM -0500, Albert Chin wrote:
> 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])
This seems to work:
p = saxexts.XMLValParserFactory.make_parser ()
p.parser.dtd = load_dtd ("[DTD File]")
p.parser.val.dtd = p.parser.dtd
p.setDocumentHandler (xmlh)
p.feed ([XML FILE AS STRING])
However, it doesn't work when using p.parseFile() instead of p.feed ().
--
albert chin (china at thewrittenword.com)
More information about the XML-SIG
mailing list