Re: SAX and DTDs

Uche Ogbuji <[email protected]>
Newsgroups gmane.comp.python.xml
Organization Fourthought, Inc.
Message-ID <1123348536.4356.28.camel@borgia>
On Thu, 2005-08-04 at 12:39 +0300, Alin Dobre wrote:
>   Hey list,
> 
> 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()

See example in listings 5 & 6 of

http://www.xml.com/pub/a/2004/11/24/py-xml.html?page=2


-- 
Uche Ogbuji                               Fourthought, Inc.
http://uche.ogbuji.net                    http://fourthought.com
http://copia.ogbuji.net                   http://4Suite.org
Use CSS to display XML, part 2 - http://www-128.ibm.com/developerworks/edu/x-dw-x-xmlcss2-i.html
XML Output with 4Suite & Amara - http://www.xml.com/pub/a/2005/04/20/py-xml.html
Use XSLT to prepare XML for import into OpenOffice Calc - http://www.ibm.com/developerworks/xml/library/x-oocalc/
Schema standardization for top-down semantic transparency - http://www-128.ibm.com/developerworks/xml/library/x-think31.html

_______________________________________________
XML-SIG maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/xml-sig
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.