XML Validation with Python

Asun Friere afriere at yahoo.co.uk
Tue Jul 29 00:05:47 EDT 2003


hwlgw at hotmail.com (Will Stuyvesant) wrote in message news:<cb035744.0307030647.3aed82b6 at posting.google.com>...

> Anyway, I can now do XML validation, below is
> "validate.py".  But I am not solving my initial
> problem: if it validates, then validate.py prints
> nothing, if there is a mistake then it prints an error
> message.  What I really wanted; giving more confidence
> that the validation is okay; is to print 1 or 0
> depending on the result, but I have not figured out yet
> how to do that and now I am too tired of it all...

This might do the trick:

# file: validate.py
import sys, pyRXP

if len(sys.argv)<2 or sys.argv[1] in ['-h','--help','/?']: 
    print 'Usage: validate.py xmlfilename'
    sys.exit()

fn = open(sys.argv[1], 'r').read()
try :
    pyRXP.Parser().parse(fn)
    print True
except pyRXP.error :
    print False


Though personally, rather than printing False, I would simply raise in
the except clause, as the traceback provides the user with more
information as to what is wrong with their xml.




More information about the Python-list mailing list