How to use the defusedxml.cElementTree.parse 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 moloch-- / RootTheBox / setup / xmlsetup.py View on Github external
def _xml_file_import(filename):
    """ Parse and import a single XML file """
    logging.debug("Processing: %s" % filename)
    try:
        tree = ET.parse(filename)
        xml_root = tree.getroot()
        levels = get_child_by_tag(xml_root, "gamelevels")
        create_levels(levels)
        categories = get_child_by_tag(xml_root, "categories")
        create_categories(categories)
        corporations = get_child_by_tag(xml_root, "corporations")
        create_corps(corporations)
        configuration = get_child_by_tag(xml_root, "configuration")
        update_configuration(configuration)
        logging.debug("Done processing: %s" % filename)
        dbsession.commit()
        return True
    except:
        dbsession.rollback()
        logging.exception(
            "Exception raised while parsing %s, rolling back changes" % filename
github moloch-- / RootTheBox / setup / importers.py View on Github external
def _xml_file_import(filename):
    ''' Parse and import a single XML file '''
    logging.info("Importing %s ... " % filename)
    try:
        tree = ET.parse(filename)
        xml_root = tree.getroot()
        levels = get_child_by_tag(xml_root, "gamelevels")
        create_levels(levels)
        corporations = get_child_by_tag(xml_root, "corporations")
        create_corps(corporations)
        logging.info("Imported %s successfully" % filename)
        return True
    except:
        logging.exception("Exception raised while parsing %s" % filename)
        return False
github google / earthenterprise / earth_enterprise / src / server / wsgi / search / plugin / custom_POI_search_handler.py View on Github external
def _RetrievePlacemarks(self, xml_data):
    """Retrieve placemarks from xml data.

    Args:
      xml_data: Query results from the Google Places database.
    Returns:
      xmlstr: XML with placemarks.
      total_placemarks: Total no. of placemarks.
    """

    xmlstr = ""
    total_results = 0
    # Perform XML parsing using cElementTree.
    root = ET.parse(xml_data).getroot()

    for element in root:
      if element.tag == "result":
        # Rename "result" tags as "Placemark" as per KML(XML) requirements.
        element.tag = "Placemark"

        for subelement in element[:]:
          # For the sake of simplicity, we presently look for
          # "name", "geometry", "icon", "vicinity" tags only in the
          # response and ignore the others.
          if subelement.tag not in self._reqd_tags:
            element.remove(subelement)
            continue

          if subelement.tag == "geometry":
            # Extract latitude and longitude coordinates.
github pyiron / pyiron / pyiron / vasp / vasprun.py View on Github external
def from_file(self, filename="vasprun.xml"):
        """
        Parsing vasprun.xml from the working directory

        Args:
            filename (str): Path to the vasprun file
        """
        if not (os.path.isfile(filename)):
            raise AssertionError()
        try:
            self.root = ETree.parse(filename).getroot()
        except ParseError:
            raise VasprunError(
                "The vasprun.xml file is either corrupted or the simulation has failed"
            )

        self.parse_root_to_dict()
github PyCQA / bandit / examples / xml_etree_celementtree.py View on Github external
import xml.etree.cElementTree as badET
import defusedxml.cElementTree as goodET

xmlString = "\nTove\nJani\nReminder\nDon't forget me this weekend!\n"

# unsafe
tree = badET.fromstring(xmlString)
print(tree)
badET.parse('filethatdoesntexist.xml')
badET.iterparse('filethatdoesntexist.xml')
a = badET.XMLParser()

# safe
tree = goodET.fromstring(xmlString)
print(tree)
goodET.parse('filethatdoesntexist.xml')
goodET.iterparse('filethatdoesntexist.xml')
a = goodET.XMLParser()
github pyiron / pyiron / pyiron / vasp / vasprun.py View on Github external
def from_file(self, filename="vasprun.xml"):
        """
        Parsing vasprun.xml from the working directory

        Args:
            filename (str): Path to the vasprun file
        """
        if not (os.path.isfile(filename)):
            raise AssertionError()
        try:
            self.root = ETree.parse(filename).getroot()
        except ParseError:
            raise VasprunError("The vasprun.xml file is either corrupted or the simulation has failed")

        self.parse_root_to_dict()
github gamesun / MyTerm / myterm.py View on Github external
def LoadSettings(self):
        if os.path.isfile(get_config_path("settings.xml")):
            with open(get_config_path("settings.xml"), 'r') as f:
                tree = safeET.parse(f)

            port = tree.findtext('GUISettings/PortConfig/port', default='')
            if port != '':
                self.cmbPort.setProperty("currentText", port)

            baudrate = tree.findtext('GUISettings/PortConfig/baudrate', default='38400')
            if baudrate != '':
                self.cmbBaudRate.setProperty("currentText", baudrate)

            databits = tree.findtext('GUISettings/PortConfig/databits', default='8')
            id = self.cmbDataBits.findText(databits)
            if id >= 0:
                self.cmbDataBits.setCurrentIndex(id)

            parity = tree.findtext('GUISettings/PortConfig/parity', default='None')
            id = self.cmbParity.findText(parity)