def validate(xml_file, dtd_file): doc = libxml2.parseFile(xml_file) dtd = libxml2.parseDTD(None, dtd_file) ctxt = libxml2.newValidCtxt() ret = doc.validateDtd(ctxt, dtd) dtd.freeDtd() doc.freeDoc() return ret
def dtd_validation(xml_file, dtd_file): doc = libxml2.parseFile(xml_file) dtd = libxml2.parseDTD(None, dtd_file) ctxt = libxml2.newValidCtxt() res = doc.validateDtd(ctxt, dtd) if res == 0: print "VALIDATION FAILED" else: print "VALIDATED" dtd.freeDtd() doc.freeDoc() return res
def validate_dtd(xml_file, dtd_file): """Проверка xml файла на соответствие dtd""" doc = libxml2.parseFile(xml_file) dtd = libxml2.parseDTD(None, dtd_file) ctxt = libxml2.newValidCtxt() res = doc.validateDtd(ctxt, dtd) if res == 1: print "Документ " + xml_file + " соответствует " + dtd_file open(xml_file) else: print "Документ " + xml_file + " НЕ соответствует " + dtd_file doc.freeDoc() dtd.freeDtd() del ctxt
def validate(xml_file, dtd_file): """Проверка xml файла на соответствие dtd""" doc = libxml2.parseFile(xml_file) dtd = libxml2.parseDTD(None, dtd_file) ctxt = libxml2.newValidCtxt() res = doc.validateDtd(ctxt, dtd) if res == 1: print "Документ " + xml_file + " соответствует " + dtd_file open(xml_file) else: print "Документ " + xml_file + " НЕ соответствует " + dtd_file doc.freeDoc() dtd.freeDtd() del ctxt
def validXML(dtd_file, xml_file): try: dtd = libxml2.parseDTD(None, dtd_file) ctxt = libxml2.newValidCtxt() doc = libxml2.parseFile(xml_file) ret = doc.validateDtd(ctxt, dtd) if ret != 1: return False doc.freeDoc() dtd.freeDtd() del dtd del ctxt except Exception as e: raise Exception("validation is failed due to " + e.message) return True
def validate_xml(dtd_file,xml_file): result = {'result':'success','message':'complete DTD validation'} try: dtd = libxml2.parseDTD(None, dtd_file) ctxt = libxml2.newValidCtxt() doc = libxml2.parseFile(xml_file) ret = doc.validateDtd(ctxt, dtd) if ret != 1: print "error doing DTD validation" result['result'] = "fail" result['message'] = "error doing DTD validation" doc.freeDoc() dtd.freeDtd() del dtd del ctxt except Exception as e: result['result'] = "fail" result['message'] = "error doing DTD validation: " + e.message return result
import libxml2 import libxml2mod import sys def error(msg, data): pass # Memory debug specific libxml2.debugMemory(1) dtd="""<!ELEMENT foo EMPTY>""" instance="""<?xml version="1.0"?> <foo></foo>""" dtd = libxml2.parseDTD(None, 'test.dtd') ctxt = libxml2.newValidCtxt() libxml2mod.xmlSetValidErrors(ctxt._o, error, error) doc = libxml2.parseDoc(instance) ret = doc.validateDtd(ctxt, dtd) if ret != 1: print "error doing DTD validation" sys.exit(1) doc.freeDoc() dtd.freeDtd() del dtd del ctxt # Memory debug specific libxml2.cleanupParser() if libxml2.debugMemory(1) == 0:
import sys def error(msg, data): pass # Memory debug specific libxml2.debugMemory(1) dtd = """<!ELEMENT foo EMPTY>""" instance = """<?xml version="1.0"?> <foo></foo>""" dtd = libxml2.parseDTD(None, 'test.dtd') ctxt = libxml2.newValidCtxt() libxml2mod.xmlSetValidErrors(ctxt._o, error, error) doc = libxml2.parseDoc(instance) ret = doc.validateDtd(ctxt, dtd) if ret != 1: print "error doing DTD validation" sys.exit(1) doc.freeDoc() dtd.freeDtd() del dtd del ctxt # Memory debug specific libxml2.cleanupParser() if libxml2.debugMemory(1) == 0: