Hi guys,

Here's a quick attempt at an XML validator in python. Note it is just that - a validator - it doesn't do any processing. Its error reporting appears limited ATM. I'm not sure if that's simply a limitation of the python bindings or whether I need a couple more lines in there to flick a 'verbose' switch on or something.

Regards,

Matt.
#!/usr/bin/env python
from optparse import OptionParser
from libxml2 import newValidCtxt, readFile, xmlDoc, treeError
import sys

def validateFile(filename):
        doc = None
        ctxt = newValidCtxt()

        try:
                doc = readFile(filename, None, 0);
        except treeError:
                print "Failed to parse file", filename
                sys.exit(1)

        if (doc.xincludeProcess() == -1):
                print "Error: XInclude processing failed"
                sys.exit(1)

        if (doc.validateDocument(ctxt) == 0):
                print "Error:", filename, "doesn't validate"
                sys.exit(1)

if __name__ == "__main__":
        filename = None
        usage = "usage: %prog filename"
        parser = OptionParser(usage=usage, version="%prog 0.1")
        (options, args) = parser.parse_args()
        if len(args) != 1:
                parser.error("Wrong number of arguments")
        filename = args[0]
        validateFile(filename)
-- 
http://linuxfromscratch.org/mailman/listinfo/alfs-discuss
FAQ: http://www.linuxfromscratch.org/faq/
Unsubscribe: See the above information page

Reply via email to