Saturday 15 January 2011

using pyqt4 to validate XML Schemas

Follow up to the problem solved in the last posting.
lxml worked great, but I don't want to burden folks with yet another 3rd party module.

so I once again looked to pyqt4 for help....
and..

from PyQt4.QtXmlPatterns import QXmlSchemaValidator, QXmlSchema

#open up the xsd file - and load it into QXMLSchema
f=open("foo.xsd", "r")
xsd = f.read()
f.close()

schema = QXmlSchema()
schema.load(xsd)

#now the xml itself.
f = open("foo.xml", "r")
xml = f.read()
f.close()

validator = QXmlSchemaValidator(schema)
print (validator.validate(xml))

#Returns True :)

No comments: