我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用lxml.etree.DTD。
def is_valid(self): """ Validate if the NZB File is okay; this will generate some overhead but at the same time it caches a lot of the results it returns so future calls will be speedy The function returns True if the nzb file is valid, otherwise it returns False """ if self._lazy_is_valid is None: if self.open(): # Open DTD file and create dtd object dtdfd = open(NZB_XML_DTD_FILE) dtd = etree.DTD(dtdfd) # Verify our dtd file against our current stream try: nzb = etree.parse(self.filepath) except XMLSyntaxError as e: if e[0] is not None: # We have corruption logger.error( "NZB-File '%s' is corrupt" % self.filepath) logger.debug( 'NZB-File XMLSyntaxError Exception %s' % str(e)) # Mark situation self._lazy_is_valid = False # We failed return False self._lazy_is_valid = dtd.validate(nzb) return self._lazy_is_valid is True
def val1_DTD(current_user, filename): # MATCH WN AGAINST DTD l=lambda:dd(l) vr = l() # validation report dtd_f = open(ILI_DTD, 'rb') try: dtd = etree.DTD(dtd_f) if filename.endswith('.xml'): wn = open(os.path.join(app.config['UPLOAD_FOLDER'], filename), 'rb') wnlmf = etree.XML(wn.read()) vr['lmf_dump'] = wn.read() elif filename.endswith('.gz'): with gzip.open(os.path.join(app.config['UPLOAD_FOLDER'], filename), 'rb') as wn: wnlmf = etree.XML(wn.read()) vr['lmf_dump'] = wn.read() vr['read'] = True if dtd.validate(wnlmf): vr['dtd_val'] = True else: vr['dtd_val'] = False vr['dtd_val_errors'] = str(dtd.error_log.filter_from_errors()[0]) except: vr['read'] = False vr['dtd_val'] = False vr['final_validation'] = False dtd_f.close() return vr
def xparse(xml_file, val_dtd): """ Function: xparse Variables: xml_file - the XML file to be reviewed val_dtd - The DTD object for validation Return: On Success: Parsed XML 'Element' On Fail: Return nothing which basically skips the file Purpose: This function takes the xml_file and parses it into a handler so that it can be evaluated. """ try: xml_handler = open(xml_file, 'r') xparse = etree.parse(xml_handler).getroot() if not val_dtd.validate(xparse): print "%s: XML file does not comply with Scalp DTD: %s" % (sys.argv[0], xml_file) return "" xml_handler.close() return xparse except IOError: print "%s: IOError with the filter's file: %s" % (sys.argv[0], xml_file) return except: print "%s: Unknown error with the filter's file: %s" % (sys.argv[0], xml_file) return ""
def help(): """ Function: help Variables: None Purpose: Print help output to stdout """ print "Scalp External XML Reporter" print "Author: Don C. Weber" print "" print "usage: ./sexr.py [-h|--help] [-V|--version] [-v xml_dtd] [-d out_directory]" print " [-t | -f | -a | -s] <xml file or directory>" print "" print " -h | --help: Print this help." print " -V | --version: Version information." print " -v: The Scalp DTD file. './scalp_xmldtd.dtd' by default." print " -d: The directory to write the output files. './' by default. Implies -t" print " -t: Text output. This will produce a indented text file which" print " will be written to 'sexr_<date.time>.<##>.txt'." print " -f: Full parse to selected output format." print " -a: Provides a count of specific attacks detected to selected" print " output format." print " -s: Provides a count of the Source IP addresses associated with" print " the specific Attack types to selected output format."
def validate_without_dtd(): # Open and read input XML. fin = codecs.open('sk.xml','r','utf-8') out = fin.read() fin.close() # Try to parse. # If there is error, it will be printed. Correct and retry. root = ET.fromstring(out.encode('utf-8')) # If there is no error, this line will be printed on screen. print "No error found in XML file." # Function to vailidate with DTD.
def validate_with_dtd(): parser = ET.XMLParser(dtd_validation=True) tree = ET.parse('sk.xml', parser) # If no error found, print this line. print "No error found in XML file." # Function to validate with DTD, with printing of errors also on screen.
def validate_with_dtd1(): dtd = ET.DTD('sk.dtd') tree = ET.parse('sk.xml') print dtd.validate(tree) # Print errors on screen for debug. print dtd.error_log.filter_from_errors()[0] # Function to read XPATH from given XML.