def validate(xml_file): """ Returns None if the xml config is valid, prints a list of validation errors otherwise. """ xml_data = xml_file.read() try: xml_doc = libxml2.parseMemory(xml_data, len(xml_data)) except libxml2.parserError: error("Could not parse '%s', check it contains valid XML " "configuration." % (xml_file.name,)) return xml_context = xml_doc.xpathNewContext() dtd = libxml2.parseDTD(None, "validxml.dtd") ret = xml_doc.validateDtd(xml_context, dtd) if ret == 0: error("XML in '%s' not valid, see error(s) above." % (xml_file.name,)) dtd.freeDtd() xml_doc.freeDoc() xml_context.xpathFreeContext() del dtd del xml_doc del xml_context xml_file.close()
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
def __init__(self, game_path=None): self.data = {} if game_path is None: game_path = get_activity_root() if isdir(game_path): self.game_path = game_path else: _logger.error('Game_path not found in %s' % game_path) return self.data['face'] = '' self.data['align'] = '1' try: self.dtd = libxml2.parseDTD(None, join(get_bundle_path(), 'memorize.dtd')) except libxml2.parserError, e: _logger.error('Init: no memorize.dtd found ' +str(e)) self.dtd = None
#!/usr/bin/python -u 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()
options.output = ['hosts', 'services'] else: options.output = [options.output,] # Read host and/or service template if options.tmpl_host and 'hosts' in options.output: HOSTTEMPL = open(options.tmpl_host).read() if options.tmpl_service and 'services' in options.output: SERVICETEMPL = open(options.tmpl_service).read() # Get URL or file doc = read_xml(options) # Check XML against DTD if options.schemacheck: dtd = libxml2.parseDTD(None, options.schemacheck) ctxt = libxml2.newValidCtxt() ret = doc.validateDtd(ctxt, dtd) if ret != 1: print "error doing DTD validation" sys.exit(1) dtd.freeDtd() del dtd del ctxt # Check XML file basics (status, statusstring) = xml_check_version(doc) debug(1, options.verb, statusstring) if not status: print statusstring
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()
#!/usr/bin/python2 -u import libxml2 import sys # 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() 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: print("OK") else: print("Memory leak %d bytes" % (libxml2.debugMemory(1))) libxml2.dumpMemory()