Python lxml.etree 模块,DocumentInvalid() 实例源码

我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用lxml.etree.DocumentInvalid()

项目:fomod-validator    作者:GandaG    | 项目源码 | 文件源码
def validate_tree(elem_tree, schema_file, ignore_errors=False):
    """
    Validate your FOMOD installer. Raises ValidationError if installer is not valid.
    :param elem_tree: The root element of your config xml tree.
    :param schema_file: The path to the schema file, with filename and extension.
    :param ignore_errors: If true, the function returns False instead of throwing an error.
    """
    try:
        xmlschema_doc = etree.parse(schema_file)
        xmlschema = etree.XMLSchema(xmlschema_doc)
        xmlschema.assertValid(elem_tree)
        return True
    except etree.ParseError as e:
        raise ParserError(str(e))
    except etree.DocumentInvalid as e:
        if ignore_errors:
            return False
        raise ValidationError("The Config tree is invalid with error message:\n\n" + str(e))
项目:qexsd    作者:QEF    | 项目源码 | 文件源码
def validate(self, xml_config):
        """
        Validate the configuration with XSD and with optional parameter dependencies.
        :param xml_config:
        """
        try:
            import lxml.etree as ET
        except ImportError:
            logger.debug("")
            return

        xsd = ET.parse(self._xsd_file)
        xml_schema = ET.XMLSchema(xsd)

        try:
            xml_schema.assertValid(xml_config.tostring())
        except AttributeError:
            logger.warning("XML Schema validation not available!")
        except ET.DocumentInvalid as e:
            raise XMLSchemaValidationError(e)
项目:cfdilib    作者:Vauxoo    | 项目源码 | 文件源码
def validate(self, schema_str, xml_valid):
        """Compare the valid information on an xml from  given schema.

        :param str schema_str: content string from schema file.
        :param str xml_valid: content string from xml file.
        :returns: If it is Valid or Not.
        :rtype: bool
        """
        # TODO: be able to get doc for error given an xsd.
        # Changed path to allow have xsd that are imported by others xsd in the
        # same library, and not call to SAT page each time that is generated
        # a new XML.
        with change_path():
            path = os.path.join(
                os.path.dirname(os.path.realpath(__file__)), 'templates')
            os.chdir(path)
            schema_root = etree.parse(StringIO(schema_str))
            schema = etree.XMLSchema(schema_root)
        try:
            tree = etree.parse(StringIO(xml_valid.encode('UTF-8')))
            schema.assertValid(tree)
        except etree.DocumentInvalid as ups:
            self.ups = ups
        finally:
            if self.ups:
                self.valid = False
            else:
                self.valid = True
            return self.valid
项目:dati-ckan-docker    作者:italia    | 项目源码 | 文件源码
def _is_valid(cls, xml, xsd_filepath, xsd_name):
        '''Returns whether or not an XML file is valid according to
        an XSD. Returns a tuple, the first value is a boolean indicating
        whether the validation passed or not. The second is a list of tuples,
        each containing the error message and the error line.

        Params:
          xml - etree of the XML to be validated
          xsd_filepath - full path to the XSD file
          xsd_name - string describing the XSD

        Returns:
          (is_valid, [(error_message_string, error_line_number)])
        '''
        xsd = etree.parse(xsd_filepath)
        schema = etree.XMLSchema(xsd)
        # With libxml2 versions before 2.9, this fails with this error:
        #    gmx_schema = etree.XMLSchema(gmx_xsd)
        # File "xmlschema.pxi", line 103, in
        # lxml.etree.XMLSchema.__init__ (src/lxml/lxml.etree.c:116069)
        # XMLSchemaParseError: local list type: A type, derived by list or
        # union, must have the simple ur-type definition as base type,
        # not '{http://www.opengis.net/gml/3.2}doubleList'., line 118
        try:
            schema.assertValid(xml)
        except etree.DocumentInvalid:
            log.info(
                'Validation errors found using schema {0}'.format(xsd_name))
            errors = []
            for error in schema.error_log:
                errors.append((error.message, error.line))
            errors.insert
            return False, errors
        return True, []
项目:tiss    作者:dudanogueira    | 项目源码 | 文件源码
def valida_estrutura(self):
        try:
            self.xsd_schema.assertValid(self.root)
            self.valido = True
            self.schema_valido = True
            self.calcula_hash()
            self.valida_hash()
        except etree.DocumentInvalid as xml_errors:
            self.valid = False
            self.schema_valido = False
            self.schema_erros = xml_errors
            i = 1
            for erro in self.schema_erros.error_log:
                self.erros['lote']['_schema_invalido%i' % i] = "Linha %s, Coluna: %s: %s" % (erro.line, erro.column, erro.message)
                i += 1
项目:aiohttp-xmlrpc    作者:mosquito    | 项目源码 | 文件源码
def _parse_response(body, method_name):
        try:
            if log.getEffectiveLevel() <= logging.DEBUG:
                log.debug("Server response: \n%s", body.decode())

            response = etree.fromstring(body)
            schema.assertValid(response)
        except etree.DocumentInvalid:
            raise ValueError("Invalid body")

        result = response.xpath('//params/param/value/*')
        if result:
            return xml2py(result[0])

        fault = response.xpath('//fault/value/*')
        if fault:
            err = xml2py(fault[0])

            raise xml2py_exception(
                err.get('faultCode', exceptions.SystemError.code),
                err.get('faultString', 'Unknown error'),
                default_exc_class=exceptions.ServerError
            )

        raise exceptions.ParseError('Respond body for method "%s" '
                                    'not contains any response.', method_name)
项目:aiohttp-xmlrpc    作者:mosquito    | 项目源码 | 文件源码
def _parse_body(self, body):
        try:
            return self._parse_xml(body)
        except etree.DocumentInvalid:
            raise HTTPBadRequest
项目:pygcam    作者:JGCRI    | 项目源码 | 文件源码
def validate(self, raiseOnError=True):
        """
        Validate a ParameterList against ``self.schemaFile``. Optionally raises an
        error on failure, else return boolean validity status. If no schema file
        is defined, return ``True``.
        """
        if not self.schemaPath:
            return True

        tree = self.tree

        # ensure that the entire directory has been extracted so that 'xs:include' works
        pkg.resource_filename('pygcam', os.path.dirname(self.schemaPath))
        abspath = pkg.resource_filename('pygcam', self.schemaPath)

        xsd = ET.parse(abspath)
        schema = ET.XMLSchema(xsd)

        if raiseOnError:
            try:
                schema.assertValid(tree)
                return True
            except ET.DocumentInvalid as e:
                raise XmlFormatError("Validation of '%s'\n  using schema '%s' failed:\n  %s" % (self.filename, self.schemaPath, e))
        else:
            valid = schema.validate(tree)
            return valid
项目:ws-cli    作者:hack4sec    | 项目源码 | 文件源码
def parse_links(self, content_type, content, link):
        """
        Method parse a page content and extract links from it. Can parse xml, html and css.
        Other content parse how plain text
        :param content_type: type of content (text/xml, application/xml, text/html, text/css or other one)
        :param content: content of page
        :param link: url of current page
        :return:
        """
        try:
            if not len(content.strip()):
                return []

            if content_type in ['text/xml', 'application/xml'] or content[:6] == '<?xml ':
                links = self.parse_links_xml(content)
                text_links = self.parse_links_text_re(content)
                links.extend(text_links)
            elif content_type == 'text/css':
                links = self.parse_links_css(content)
                text_links = self.parse_links_text_re(content)
                links.extend(text_links)
            elif content_type == 'text/html':
                links = self.parse_links_html_re(content)
                text_links = self.parse_links_text_re(content)
                links.extend(text_links)
            else:
                links = self.parse_links_html_re(content)
                text_links = self.parse_links_text_re(content)
                links.extend(text_links)

            if Registry().isset('ignore_regexp'):
                links = self._clear_ignore(links)

            if Registry().isset('only_one'):
                links = self._clear_only_one(links)

            if Registry().get('config')['spider']['denied_schemes']:
                links = self._clear_by_schema(links)

        except etree.XMLSyntaxError:
            links = self.parse_links_text_re(content)
            Registry().get('logger').log(
                " Document syntax error {0}, parsing as text"
                .format(link['path'] + '?' + link['query'] if len(link['query']) else link['path'])
            )
        except etree.DocumentInvalid:
            links = self.parse_links_text_re(content)
            Registry().get('logger').log(
                " Document invalid {0}, parsing as text"
                .format(link['path'] + '?' + link['query'] if len(link['query']) else link['path'])
            )

        return list(set(links))