How to use the defusedxml.lxml.fromstring function in defusedxml

To help you get started, we’ve selected a few defusedxml examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github GeoNode / geonode / geonode / maps / tests.py View on Github external
def test_map_to_wmc(self):
        """ /maps/1/wmc -> Test map WMC export
            Make some assertions about the data structure produced
            for serialization to a Web Map Context Document
        """

        map_obj = Map.objects.all().first()
        map_obj.set_default_permissions()
        response = self.client.get(reverse('map_wmc', args=(map_obj.id,)))
        self.assertEqual(response.status_code, 200)

        # check specific XPaths
        wmc = dlxml.fromstring(response.content)

        namespace = '{http://www.opengis.net/context}'
        title = '{ns}General/{ns}Title'.format(ns=namespace)
        abstract = '{ns}General/{ns}Abstract'.format(ns=namespace)

        self.assertIsNotNone(wmc.attrib.get('id'))
        self.assertEqual(wmc.find(title).text, 'GeoNode Default Map')
        self.assertEqual(
            wmc.find(abstract).text,
            'GeoNode default map abstract')
github aruhier / virt-backup / tests / helper / virt_backup.py View on Github external
def __init__(self, _conn, name="test", id=1, *args, **kwargs):
        self._conn = _conn
        self._state = [1, 1]

        with open(os.path.join(CUR_PATH, "testdomain.xml")) as dom_xmlfile:
            self.dom_xml = defusedxml.lxml.fromstring(dom_xmlfile.read())
        self.set_id(id)
        self.set_name(name)
github GeoNode / geonode / geonode / geoserver / helpers.py View on Github external
# read GWC configuration
    req, content = http_client.get(
        url,
        headers=headers,
        user=_user)
    if req.status_code != 200:
        line = "Error {0} reading Style Filter Params GeoWebCache at {1}".format(
            req.status_code, url
        )
        logger.error(line)
        return

    # check/write GWC filter parameters
    import xml.etree.ElementTree as ET
    body = None
    tree = dlxml.fromstring(_)
    param_filters = tree.findall('parameterFilters')
    if param_filters and len(param_filters) > 0:
        if not param_filters[0].findall('styleParameterFilter'):
            style_filters_xml = "STYLES\
                "
            style_filters_elem = dlxml.fromstring(style_filters_xml)
            param_filters[0].append(style_filters_elem)
            body = ET.tostring(tree)
    if body:
        req, content = http_client.post(
            url,
            data=body,
            headers=headers,
            user=_user)
        if req.status_code != 200:
            line = "Error {0} writing Style Filter Params GeoWebCache at {1}".format(
github greenbone / python-gvm / gvm / xml.py View on Github external
Arguments:
        xml (str, List[lxml.etree.Element] or lxml.etree.Element):
            xml as string,
            List[lxml.etree.Element] or directly a lxml element.

    """
    if isinstance(xml, list):
        for item in xml:
            if etree.iselement(item):
                print(etree.tostring(item, pretty_print=True).decode("utf-8"))
            else:
                print(item)
    elif etree.iselement(xml):
        print(etree.tostring(xml, pretty_print=True).decode("utf-8"))
    elif isinstance(xml, str):
        tree = secET.fromstring(xml)
        print(etree.tostring(tree, pretty_print=True).decode("utf-8"))
github mvantellingen / python-zeep / src / zeep / loader.py View on Github external
:type settings: zeep.settings.Settings
    :returns: The document root
    :rtype: lxml.etree._Element

    """
    settings = settings or Settings()
    recover = not settings.strict
    parser = etree.XMLParser(
        remove_comments=True,
        resolve_entities=False,
        recover=recover,
        huge_tree=settings.xml_huge_tree,
    )
    parser.resolvers.add(ImportResolver(transport))
    try:
        return fromstring(
            content,
            parser=parser,
            base_url=base_url,
            forbid_dtd=settings.forbid_dtd,
            forbid_entities=settings.forbid_entities,
        )
    except etree.XMLSyntaxError as exc:
        raise XMLSyntaxError(
            "Invalid XML content received (%s)" % exc.msg, content=content
        )
github NYPL-Simplified / circulation / api / saml / parser.py View on Github external
def _convert_xml_string_to_dom(self, xml_metadata):
        """Converts an XML string containing SAML metadata into XML DOM

        :param xml_metadata: XML string containing SAML metadata
        :type xml_metadata: string

        :return: XML DOM tree containing SAML metadata
        :rtype: defusedxml.lxml.RestrictedElement

        :raise: MetadataParsingError
        """
        self._logger.debug('Started converting XML string containing SAML metadata into XML DOM')

        try:
            metadata_dom = fromstring(xml_metadata, forbid_dtd=True)
        except (ValueError, XMLSyntaxError,) as exception:
            self._logger.exception(
                'An unhandled exception occurred during converting XML string containing SAML metadata into XML DOM')

            raise SAMLMetadataParsingError(inner_exception=exception)

        self._logger.debug('Finished converting XML string containing SAML metadata into XML DOM')

        return metadata_dom
github aruhier / virt-backup / virt_backup / backups / complete.py View on Github external
def _parse_dom_xml(self):
        return defusedxml.lxml.fromstring(self.dom_xml)
github aruhier / virt-backup / virt_backup / backups / pending.py View on Github external
def _parse_dom_xml(self):
        """
        Parse the domain's definition
        """
        return defusedxml.lxml.fromstring(self.dom.XMLDesc())