Python xml.etree.cElementTree 模块,fromstring() 实例源码

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

项目:Cite    作者:siudej    | 项目源码 | 文件源码
def startApp():
    """ Initialize the application. """
    global settings, window
    from config import ConfigManager
    settings = ConfigManager()
    settings.set_default('citeheight', 500)
    settings.set_default('citewidth', 700)
    settings.set_default('citex', 0)
    settings.set_default('citey', 0)
    try:
        path = os.path.dirname(os.path.realpath(__file__))
        with open(path + '/settings.xml', 'rb') as f:
            root = et.fromstring(f.read())
            settings.setXMLConfig(root)
            settings.set_defaults(settings.config)
    except:
        pass
    import sys
    app = QApplication(sys.argv)
    window = Cite()
    sys.exit(app.exec_())
项目:weichatpay    作者:river291    | 项目源码 | 文件源码
def FromXml(self, xml):
        if not xml:
            raise WxPayException("xml?????")

        # ??ElementTree
        try:
            import xml.etree.cElementTree as ET
        except ImportError:
            import xml.etree.ElementTree as ET
        # ??xml
        xml2dict = {}

        xml_tree = ET.fromstring(xml)
        for child_node in xml_tree:
            xml2dict[child_node.tag] = child_node.text

        self.values = xml2dict
        return self.values

    # ?????????url??
项目:GAMADV-XTD    作者:taers232c    | 项目源码 | 文件源码
def CreateClassFromXMLString(target_class, xml_string, string_encoding=None):
  """Creates an instance of the target class from the string contents.

  Args:
    target_class: class The class which will be instantiated and populated
        with the contents of the XML. This class must have a _tag and a
        _namespace class variable.
    xml_string: str A string which contains valid XML. The root element
        of the XML string should match the tag and namespace of the desired
        class.
    string_encoding: str The character encoding which the xml_string should
        be converted to before it is interpreted and translated into
        objects. The default is None in which case the string encoding
        is not changed.

  Returns:
    An instance of the target class with members assigned according to the
    contents of the XML - or None if the root XML tag and namespace did not
    match those of the target class.
  """
  encoding = string_encoding or XML_STRING_ENCODING
  if encoding and isinstance(xml_string, unicode):
    xml_string = xml_string.encode(encoding)
  tree = ElementTree.fromstring(xml_string)
  return _CreateClassFromElementTree(target_class, tree)
项目:GAMADV-XTD    作者:taers232c    | 项目源码 | 文件源码
def parse(xml_string, target_class=None, version=1, encoding=None):
  """Parses the XML string according to the rules for the target_class.

  Args:
    xml_string: str or unicode
    target_class: XmlElement or a subclass. If None is specified, the
        XmlElement class is used.
    version: int (optional) The version of the schema which should be used when
        converting the XML into an object. The default is 1.
    encoding: str (optional) The character encoding of the bytes in the
        xml_string. Default is 'UTF-8'.
  """
  if target_class is None:
    target_class = XmlElement
  if isinstance(xml_string, unicode):
    if encoding is None:
      xml_string = xml_string.encode(STRING_ENCODING)
    else:
      xml_string = xml_string.encode(encoding)
  tree = ElementTree.fromstring(xml_string)
  return _xml_element_from_tree(tree, target_class, version)
项目:PySIGNFe    作者:thiagopena    | 项目源码 | 文件源码
def _le_xml(self, arquivo):
        if arquivo is None:
            return False

        if not isinstance(arquivo, basestring):
            arquivo = etree.tounicode(arquivo)

        if arquivo is not None:
            if isinstance(arquivo, basestring): 
                if NAMESPACE_NFSE in arquivo:
                    arquivo = por_acentos(arquivo)
                if u'<' in arquivo:
                    self._xml = etree.fromstring(tira_abertura(arquivo))
                else:
                    arq = open(arquivo)
                    txt = ''.join(arq.readlines())
                    txt = tira_abertura(txt)
                    arq.close()
                    self._xml = etree.fromstring(txt)
            else:
                self._xml = etree.parse(arquivo)
            return True

        return False
项目:PySIGNFe    作者:thiagopena    | 项目源码 | 文件源码
def validar(self):
        arquivo_esquema = self.caminho_esquema + self.arquivo_esquema

        # Aqui é importante remover a declaração do encoding
        # para evitar erros de conversão unicode para ascii
        xml = tira_abertura(self.xml).encode(u'utf-8')

        esquema = etree.XMLSchema(etree.parse(arquivo_esquema))

        if not esquema.validate(etree.fromstring(xml)):
            for e in esquema.error_log:
                if e.level == 1:
                    self.alertas.append(e.message.replace('{http://www.portalfiscal.inf.br/nfe}', ''))
                elif e.level == 2:
                    self.erros.append(e.message.replace('{http://www.portalfiscal.inf.br/nfe}', ''))

        return esquema.error_log
项目:radar    作者:amoose136    | 项目源码 | 文件源码
def read_array(self, dtype, count=-1, sep=""):
        """Return numpy array from file.

        Work around numpy issue #2230, "numpy.fromfile does not accept
        StringIO object" https://github.com/numpy/numpy/issues/2230.

        """
        try:
            return numpy.fromfile(self._fh, dtype, count, sep)
        except IOError:
            if count < 0:
                size = self._size
            else:
                size = count * numpy.dtype(dtype).itemsize
            data = self._fh.read(size)
            return numpy.fromstring(data, dtype, count, sep)
项目:pyuniprot    作者:cebel    | 项目源码 | 文件源码
def insert_entries(self, entries_xml, taxids=None):
        """Inserts UniProt entries from XML

        :param str entries_xml: XML string
        :param Optional[list[int]] taxids: NCBI taxonomy IDs
        """

        entries = etree.fromstring(entries_xml)
        del entries_xml

        for entry in entries:
            self.insert_entry(entry, taxids)
            entry.clear()
            del entry

        entries.clear()
        del entries

        self.session.commit()

    # profile
项目:imcsdk    作者:CiscoUcs    | 项目源码 | 文件源码
def extract_root_elem(xml_str):
    """
    extracts root xml element from xml string.

    Args:
        xml_str (str): xml string

    Returns:
        xml element

    Example:
        xml_str='''
        <lsClone dn="org-root/ls-testsp" inHierarchical="false"
        inServerName="test" inTargetOrg="">
        </lsClone>
        '''
        root_element = extract_root_elem(xml_str)
    """

    xml_str = xml_str.strip("\x00")
    root_elem = ET.fromstring(xml_str)
    return root_elem
项目:inter    作者:rsms    | 项目源码 | 文件源码
def _extractWOFFMetadata(source, destination):
    if source.metaData is None:
        return
    metadata = ElementTree.fromstring(source.metaData)
    for element in metadata:
        if element.tag == "uniqueid":
            _extractWOFFMetadataUniqueID(element, destination)
        elif element.tag == "vendor":
            _extractWOFFMetadataVendor(element, destination)
        elif element.tag == "credits":
            _extractWOFFMetadataCredits(element, destination)
        elif element.tag == "description":
            _extractWOFFMetadataDescription(element, destination)
        elif element.tag == "license":
            _extractWOFFMetadataLicense(element, destination)
        elif element.tag == "copyright":
            _extractWOFFMetadataCopyright(element, destination)
        elif element.tag == "trademark":
            _extractWOFFMetadataTrademark(element, destination)
        elif element.tag == "licensee":
            _extractWOFFMetadataLicensee(element, destination)
        elif element.tag == "extension":
            _extractWOFFMetadataExtension(element, destination)
项目:nsc-cloudproject-s22016    作者:agitaretech    | 项目源码 | 文件源码
def convert_response_to_feeds(response, convert_func):

        if response is None:
            return None

        feeds = _list_of(Feed)

        _set_continuation_from_response_headers(feeds, response)

        root = ETree.fromstring(response.body)

        # some feeds won't have the 'feed' element, just a single 'entry' element
        root_name = _get_etree_tag_name_without_ns(root.tag)
        if root_name == 'feed':
            entries = root.findall("./atom:entry", _etree_entity_feed_namespaces)
        elif root_name == 'entry':
            entries = [root]
        else:
            raise NotImplementedError()

        for entry in entries:
            feeds.append(convert_func(entry))

        return feeds
项目:nsc-cloudproject-s22016    作者:agitaretech    | 项目源码 | 文件源码
def convert_response_to_feeds(response, convert_func):

        if response is None:
            return None

        feeds = _list_of(Feed)

        _set_continuation_from_response_headers(feeds, response)

        root = ETree.fromstring(response.body)

        # some feeds won't have the 'feed' element, just a single 'entry' element
        root_name = _get_etree_tag_name_without_ns(root.tag)
        if root_name == 'feed':
            entries = root.findall("./atom:entry", _etree_entity_feed_namespaces)
        elif root_name == 'entry':
            entries = [root]
        else:
            raise NotImplementedError()

        for entry in entries:
            feeds.append(convert_func(entry))

        return feeds
项目:pytorch_fnet    作者:AllenCellModeling    | 项目源码 | 文件源码
def metadata(self):
        """Return data from MetadataSegment as xml.ElementTree root Element.

        Return None if no Metadata segment is found.

        """
        if self.header.metadata_position:
            segment = Segment(self._fh, self.header.metadata_position)
            if segment.sid == MetadataSegment.SID:
                data = segment.data().data()
                return etree.fromstring(data.encode('utf-8'))
        warnings.warn("Metadata segment not found")
        try:
            metadata = next(self.segments(MetadataSegment.SID))
            return etree.fromstring(metadata.data().encode('utf-8'))
        except StopIteration:
            pass
项目:EasyStorj    作者:lakewik    | 项目源码 | 文件源码
def save_bridge_api_url(self, bridge_api_url):

        with open(CONFIG_FILE, 'r') as conf_file:
            XML_conf_data = conf_file.read().replace('\n', '')

        root = ET.fromstring(XML_conf_data)
        client = root.find('client')
        b_api_url = client.find('bridge_api_url')
        b_api_url.set('url', str(bridge_api_url))

        #print ET.tostring(root)

        tree = ET.ElementTree(root)
        tree.write(CONFIG_FILE)


        #soup = Soup(XML_conf_data)
        #bridge_api_url_tag = soup.configuration.client.bridge_api_url
        #bridge_api_url_tag['url'] = str(bridge_api_url)

        return True
项目:tv_grab_es_movistartv    作者:MovistarTV    | 项目源码 | 文件源码
def __get_channels(xml_channels):
        root = ElTr.fromstring(xml_channels.replace('\n', ' '))
        services = root[0][0].findall("{urn:dvb:ipisdns:2006}SingleService")
        channel_list = {}
        for i in services:
            channel_id = 'unknown'
            try:
                channel_id = i[1].attrib['ServiceName']
                channel_list[channel_id] = {
                    'id': channel_id,
                    'address': i[0][0].attrib['Address'],
                    'port': i[0][0].attrib['Port'],
                    'name': i[2][0].text,
                    'shortname': i[2][1].text,
                    'genre': i[2][3][0].text,
                    'logo_uri': i[1].attrib['logoURI'] if 'logoURI' in i[1].attrib else 'MAY_1/imSer/4146.jpg'}
                if i[2][4].tag == '{urn:dvb:ipisdns:2006}ReplacementService':
                    channel_list[channel_id]['replacement'] = i[2][4][0].attrib['ServiceName']
            except KeyError as ex:
                logger.debug('El canal %s no tiene la estructura correcta: %s' % (channel_id, ex.args))
        logger.info('Canales: %i' % len(channel_list))
        return channel_list
项目:tv_grab_es_movistartv    作者:MovistarTV    | 项目源码 | 文件源码
def __get_packages(xml):
        root = ElTr.fromstring(xml.replace('\n', ' '))
        packages = root[0].findall("{urn:dvb:ipisdns:2006}Package")
        package_list = {}
        for package in packages:
            package_name = 'unknown'
            try:
                package_name = package[0].text
                package_list[package_name] = {
                    'id': package.attrib['Id'],
                    'name': package_name,
                    'services': {}}
                for service in package:
                    if not service.tag == '{urn:dvb:ipisdns:2006}PackageName':
                        service_id = service[0].attrib['ServiceName']
                        package_list[package_name]['services'][service_id] = service[1].text
            except KeyError:
                logger.error('El paquete %s no tiene la estructura correcta' % package_name)
        logger.info('Paquetes: %i' % len(package_list))
        return package_list
项目:tv_grab_es_movistartv    作者:MovistarTV    | 项目源码 | 文件源码
def __get_segments(xml):
        root = ElTr.fromstring(xml.replace('\n', ' '))
        payloads = root[0][1][1].findall("{urn:dvb:ipisdns:2006}DVBBINSTP")
        segment_list = {}
        for segments in payloads:
            source = 'unknown'
            try:
                source = segments.attrib['Source']
                segment_list[source] = {
                    'Source': source,
                    'Port': segments.attrib['Port'],
                    'Address': segments.attrib['Address'],
                    'Segments': {}}
                for segment in segments[0]:
                    segment_id = segment.attrib['ID']
                    segment_list[source]['Segments'][segment_id] = segment.attrib['Version']
            except KeyError:
                logger.error('El segment %s no tiene la estructura correcta' % source)
        logger.info('Días de EPG: %i' % len(segment_list))
        return segment_list
项目:mobileinsight-core    作者:mobile-insight    | 项目源码 | 文件源码
def __callback_wcdma_rrc_ota(self, event):
        log_item = event.data
        # log_xml = ET.fromstring(log_item["Msg"])
        log_xml = ET.XML(log_item["Msg"])

        mib = None
        sib3 = None
        for val in log_xml.iter("field"):
            if val.get("name") == "rrc.MasterInformationBlock_element":
                mib = val
            if val.get("name") == "rrc.SysInfoType3_element":
                sib3 = val

        if mib is not None:
            self.__callback_wcdma_rrc_ota_mib(event, mib)

        if sib3 is not None:
            self.__callback_wcdma_rrc_ota_sib3(event, sib3)
项目:django-modern-rpc    作者:alorence    | 项目源码 | 文件源码
def test_xrpc_invalid_request_missing_method_name(live_server):
    invalid_payload = '''<?xml version="1.0"?>
<methodCall>
  <params>
     <param>
        <value><double>2.41</double></value>
     </param>
  </params>
</methodCall>'''
    headers = {'content-type': 'text/xml'}
    response = requests.post(live_server.url + '/all-rpc/', data=invalid_payload, headers=headers)

    tree = ET.fromstring(response.content)
    members = tree.find('fault').find('value').find('struct')

    code, message = '', ''
    for member in members:
        if member.find('name').text == 'faultCode':
            code = int(member.find('value').find('int').text)
        elif member.find('name').text == 'faultString':
            message = member.find('value').find('string').text

    assert 'Missing methodName' in message
    assert code == RPC_INVALID_REQUEST
项目:django-modern-rpc    作者:alorence    | 项目源码 | 文件源码
def test_xrpc_invalid_request_but_valid_xml(live_server):
    invalid_payload = '''<?xml version="1.0"?>
<methodCall>
</methodCall>'''
    headers = {'content-type': 'text/xml'}
    response = requests.post(live_server.url + '/all-rpc/', data=invalid_payload, headers=headers)

    tree = ET.fromstring(response.content)
    members = tree.find('fault').find('value').find('struct')

    code, message = '', ''
    for member in members:
        if member.find('name').text == 'faultCode':
            code = int(member.find('value').find('int').text)
        elif member.find('name').text == 'faultString':
            message = member.find('value').find('string').text

    assert 'Invalid request: Bad XML-RPC payload' in message
    assert code == RPC_INVALID_REQUEST
项目:django-modern-rpc    作者:alorence    | 项目源码 | 文件源码
def test_xrpc_invalid_request_json_request(live_server):
    invalid_payload = json.dumps({
        "method": 'add',
        "params": [5, 6],
        "jsonrpc": "2.0",
        "id": 42,
    })
    headers = {'content-type': 'text/xml'}
    response = requests.post(live_server.url + '/all-rpc/', data=invalid_payload, headers=headers)

    tree = ET.fromstring(response.content)
    members = tree.find('fault').find('value').find('struct')

    code, message = '', ''
    for member in members:
        if member.find('name').text == 'faultCode':
            code = int(member.find('value').find('int').text)
        elif member.find('name').text == 'faultString':
            message = member.find('value').find('string').text

    assert 'not well-formed' in message
    assert code == RPC_PARSE_ERROR
项目:django-modern-rpc    作者:alorence    | 项目源码 | 文件源码
def test_xrpc_invalid_request_bad_type_value(live_server):
    invalid_payload = '''<?xml version="1.0"?>
<methodCall>
  <methodName>examples.getStateName</methodName
  <params>
     <param>
        <value><double>2.41</double></value>
     </param>
  </params>
</methodCall>'''
    headers = {'content-type': 'text/xml'}
    response = requests.post(live_server.url + '/all-rpc/', data=invalid_payload, headers=headers)

    tree = ET.fromstring(response.content)
    members = tree.find('fault').find('value').find('struct')

    code, message = '', ''
    for member in members:
        if member.find('name').text == 'faultCode':
            code = int(member.find('value').find('int').text)
        elif member.find('name').text == 'faultString':
            message = member.find('value').find('string').text

    assert 'not well-formed' in message
    assert code == RPC_PARSE_ERROR
项目:OpenTimelineIO    作者:PixarAnimationStudios    | 项目源码 | 文件源码
def test_backreference_generator_read(self):
        with open(SIMPLE_XML_PATH, 'r') as fo:
            text = fo.read()

        adapt_mod = otio.adapters.from_name('fcp_xml').module()

        tree = cElementTree.fromstring(text)
        track = adapt_mod._get_top_level_tracks(tree)[0]

        # make sure that element_map gets populated by the function calls in
        # the way we want
        element_map = collections.defaultdict(dict)

        self.assertEqual(adapt_mod._parse_rate(track, element_map), 30.0)
        self.assertEqual(track, element_map["all_elements"]["sequence-1"])
        self.assertEqual(adapt_mod._parse_rate(track, element_map), 30.0)
        self.assertEqual(track, element_map["all_elements"]["sequence-1"])
        self.assertEqual(len(element_map["all_elements"].keys()), 1)
项目:Cite    作者:siudej    | 项目源码 | 文件源码
def getSettings():
    """ Read settings from xml file. """
    global settings
    from collections import defaultdict
    settings = defaultdict(str)
    try:
        path = os.path.dirname(os.path.realpath(__file__))
        with open(path + '/settings.xml') as f:
            xml = f.read()
        import xml.etree.cElementTree as et
        et = et.fromstring(xml)[0]
        for e in et:
            # change builtin type name to the named type
            try:
                conv = convert[e.attrib['type']]
                settings[e.attrib['id']] = conv(e.text)
            except:
                pass
    except:
        print "No settings file. Continuing with a minimal configuration. "
    settings['fetcher'] = Batch
    # use batch formatting
    settings["type"] = 'batch'
    settings["html"] = False
    settings["count"] = 0
项目:gluster-nagios-common    作者:gluster    | 项目源码 | 文件源码
def _parsePeerStatus_empty_test(self):
        out = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cliOutput>
  <opRet>0</opRet>
  <opErrno>0</opErrno>
  <opErrstr>No peers present</opErrstr>
  <peerStatus/>
</cliOutput>
"""
        tree = etree.fromstring(out)
        hostList = \
            gcli._parsePeerStatus(tree, 'fedora-16-test',
                                  '711d2887-3222-46d8-801a-7e3f646bdd4d',
                                  gcli.HostStatus.CONNECTED)
        self.assertEquals(hostList,
                          [{'hostname': 'fedora-16-test',
                            'uuid': '711d2887-3222-46d8-801a-7e3f646bdd4d',
                            'status': gcli.HostStatus.CONNECTED}])
项目:LibScanner    作者:DanBeard    | 项目源码 | 文件源码
def parse_dbs(folder):
    """
    parse the XML dbs and build an in-memory lookup
    :param folder: the folder full of *.xml files
    :return:
    """
    root = None
    for filename in glob.glob(folder+'/*.xml'):
        with open(filename) as f:
            db_string = f.read() # remove the annoying namespace
            db_string = re.sub(' xmlns="[^"]+"', '', db_string, count=1)
            # xmlstring.append(db_string)
            data = ET.fromstring(db_string)
            if root is None:
                root = data
            else:
                root.extend(data)

    return root


#root = ET.fromstring("\n".join(xmlstring))
# namespace ="http://nvd.nist.gov/feeds/cve/1.2"
项目:LibScanner    作者:DanBeard    | 项目源码 | 文件源码
def get_packages_swid(package_list):
    """
    Get the packages from a swid string
    :param package_strs:
    :return:
    """
    package_xml = None
    packages = defaultdict(set)
    errors = []
    for xml_doc in package_list.split("\n"):
        try:
            # remove the <? ?> if any
            xml_doc = re.sub('<\?[^>]+\?>', '', xml_doc)
            # use DET since this is untrusted data
            data = DET.fromstring(xml_doc)
            name, version = data.attrib['name'], data.attrib['version']
            version = version.split("-")[0]
            packages[name].add(version)

        except Exception as e:
            errors.append(str(e))

    return errors, packages
项目:wechat    作者:zgliujiangang    | 项目源码 | 文件源码
def clear_response(self, xml, args=None):
        # ??????,????
        xml_tree = ET.fromstring(xml)
        try:
            msg_type = xml_tree.find("MsgType").text
        except AttributeError:
            msg_type = ""
        try:
            event = xml_tree.find("Event").text
        except AttributeError:
            event = ""
        try:
            _msg_type = self.convert((msg_type, event))
            return self.register_funcs[_msg_type](xml_tree) or self.default_response(xml_tree)
        except KeyError as e:
            logging.error(str(e))
            return self.default_response(xml_tree)
        except Exception as e:
            logging.error(str(e))
            raise e
项目:splunk_ta_ps4_f1_2016    作者:jonathanvarley    | 项目源码 | 文件源码
def list_collection(self, collection=None, app=None, owner="nobody"):
        """
        :collection: collection name. When euqals "None", return all
        collections in the system.
        :return: a list containing the connection names if successful, throws
        KVNotExists if no such colection or other exception if other error
        happened
        """

        uri = self._get_config_endpoint(app, owner, collection)

        content = self._do_request(uri, method="GET")
        m = re.search(r'xmlns="([^"]+)"', content)
        path = "./entry/title"
        if m:
            ns = m.group(1)
            path = "./{%s}entry/{%s}title" % (ns, ns)

        collections = et.fromstring(content)
        return [node.text for node in collections.iterfind(path)]
项目:TA-SyncKVStore    作者:georgestarcher    | 项目源码 | 文件源码
def list_collection(self, collection=None, app=None, owner="nobody"):
        """
        :collection: collection name. When euqals "None", return all
        collections in the system.
        :return: a list containing the connection names if successful, throws
        KVNotExists if no such colection or other exception if other error
        happened
        """

        uri = self._get_config_endpoint(app, owner, collection)

        content = self._do_request(uri, method="GET")
        m = re.search(r'xmlns="([^"]+)"', content)
        path = "./entry/title"
        if m:
            ns = m.group(1)
            path = "./{%s}entry/{%s}title" % (ns, ns)

        collections = et.fromstring(content)
        return [node.text for node in collections.iterfind(path)]
项目:cb-defense-splunk-app    作者:carbonblack    | 项目源码 | 文件源码
def list_collection(self, collection=None, app=None, owner="nobody"):
        """
        :collection: collection name. When euqals "None", return all
        collections in the system.
        :return: a list containing the connection names if successful, throws
        KVNotExists if no such colection or other exception if other error
        happened
        """

        uri = self._get_config_endpoint(app, owner, collection)

        content = self._do_request(uri, method="GET")
        m = re.search(r'xmlns="([^"]+)"', content)
        path = "./entry/title"
        if m:
            ns = m.group(1)
            path = "./{%s}entry/{%s}title" % (ns, ns)

        collections = et.fromstring(content)
        return [node.text for node in collections.iterfind(path)]
项目:CirnoBot    作者:tehnotcpu    | 项目源码 | 文件源码
def getdict(self):
        username = config['API']['mal']
        response = requests.get('http://myanimelist.net/'
                                'malappinfo.php?status=all&u=%s' % username)

        result = {}
        for raw_entry in ET.fromstring(response.text):
            entry = {attr.tag: attr.text for attr in raw_entry}

            if 'series_title' in entry:
                entry_id = entry['series_title']

                result[entry_id] = {
                    'score': int(entry['my_score']),
                    'episode': int(entry['my_watched_episodes']),
                    'start': entry['my_start_date'],
                    'end': entry['my_finish_date']
                }

        return result
项目:plugin.video.streamondemand-pureita    作者:orione7    | 项目源码 | 文件源码
def CreateClassFromXMLString(target_class, xml_string, string_encoding=None):
  """Creates an instance of the target class from the string contents.

  Args:
    target_class: class The class which will be instantiated and populated
        with the contents of the XML. This class must have a _tag and a
        _namespace class variable.
    xml_string: str A string which contains valid XML. The root element
        of the XML string should match the tag and namespace of the desired
        class.
    string_encoding: str The character encoding which the xml_string should
        be converted to before it is interpreted and translated into 
        objects. The default is None in which case the string encoding
        is not changed.

  Returns:
    An instance of the target class with members assigned according to the
    contents of the XML - or None if the root XML tag and namespace did not
    match those of the target class.
  """
  encoding = string_encoding or XML_STRING_ENCODING
  if encoding and isinstance(xml_string, unicode):
    xml_string = xml_string.encode(encoding)
  tree = ElementTree.fromstring(xml_string)
  return _CreateClassFromElementTree(target_class, tree)
项目:ucscsdk    作者:CiscoUcs    | 项目源码 | 文件源码
def extract_root_elem(xml_str):
    """
    extracts root xml element from xml string.

    Args:
        xml_str (str): xml string

    Returns:
        xml element

    Example:
        xml_str='''
        <lsClone dn="org-root/ls-testsp" inHierarchical="false"
        inServerName="test" inTargetOrg="">
        </lsClone>
        '''
        root_element = extract_root_elem(xml_str)
    """

    root_elem = ET.fromstring(xml_str)
    return root_elem
项目:treadmill    作者:Morgan-Stanley    | 项目源码 | 文件源码
def list_set(target_set):
    """List members of the set.

    :param ``str`` target_set:
        Name of the IPSet set to list.
    :returns:
        ``list`` -- List of the set member IPs as strings.
    """
    (_res, output) = _ipset(
        'list',
        '-o', 'xml',
        target_set
    )
    # Extract the members from the xml output
    et = etree.fromstring(output)
    return [
        c.text
        for c in et.find('members')
    ]
项目:Line-Chatbot    作者:zake7749    | 项目源码 | 文件源码
def parseXMLReport(self, report, location):

        """
        ?? XML??????????????location ?????
        """

        xml_namespace = "{urn:cwb:gov:tw:cwbcommon:0.1}"
        root = et.fromstring(report)
        dataset = root.find(xml_namespace + 'dataset')
        locations_info = dataset.findall(xml_namespace + 'location')

        # ?? <location> Elements,?? location ?????????
        target_idx = -1
        for idx,ele in enumerate(locations_info):
            locationName = ele[0].text # ?????
            if locationName == location:
                target_idx = idx
                break

        # ??????? location ?????
        weather_element = locations_info[target_idx][1] # ?? Wx (????)
        block_of_current_time = weather_element[1] # ??????????
        description = block_of_current_time[2][0].text
        return description
项目:qualisys_python_sdk    作者:qualisys    | 项目源码 | 文件源码
def on_connect(self, connection, version):
        print('Connected to QTM with {}'.format(version))
        # Connection is the object containing all methods/commands you can send to qtm
        self.connection = connection

        try:
            # By yielding we return control to the twisted library and execution of this function will
            # continue when we have received a reply, or an error
            # Errors will result in a QRTCommandException
            result = yield self.connection.get_parameters(parameters=['3d'])
        except QRTCommandException as e:
            self.fail(e.value)
            return

        # Parse the returned xml
        xml = ET.fromstring(result)
        self.labels = [label.text for label in xml.iter('Name')]

        # Get state to try and figure out if we're already doing rt from file
        self.connection.get_state()
项目:nagios-plugins-gluster    作者:gluster    | 项目源码 | 文件源码
def test_getLatestStat_success(self, utcnow_mock):
        expectedDict = {'cpu-load': {'cpu': {'idle': '96.62',
                                             'iowait': '0.17',
                                             'nice': '0.00',
                                             'number': 'all',
                                             'steal': '0.00',
                                             'system': '1.20',
                                             'user': '2.01'}},
                        'date': '2014-03-19',
                        'interval': '60',
                        'time': '10:19:01',
                        'utc': '1'}
        utcnow_mock.return_value = datetime(2014, 3, 19, 10, 19, 22,
                                            164227)
        with open("getLatestStat_success.xml") as f:
            out = f.read()
            tree = etree.fromstring(out)
        outDict = plugins.sadf.getLatestStat(tree)
        self.assertEquals(expectedDict, outDict)
项目:GAMADV-X    作者:taers232c    | 项目源码 | 文件源码
def CreateClassFromXMLString(target_class, xml_string, string_encoding=None):
  """Creates an instance of the target class from the string contents.

  Args:
    target_class: class The class which will be instantiated and populated
        with the contents of the XML. This class must have a _tag and a
        _namespace class variable.
    xml_string: str A string which contains valid XML. The root element
        of the XML string should match the tag and namespace of the desired
        class.
    string_encoding: str The character encoding which the xml_string should
        be converted to before it is interpreted and translated into
        objects. The default is None in which case the string encoding
        is not changed.

  Returns:
    An instance of the target class with members assigned according to the
    contents of the XML - or None if the root XML tag and namespace did not
    match those of the target class.
  """
  encoding = string_encoding or XML_STRING_ENCODING
  if encoding and isinstance(xml_string, unicode):
    xml_string = xml_string.encode(encoding)
  tree = ElementTree.fromstring(xml_string)
  return _CreateClassFromElementTree(target_class, tree)
项目:GAMADV-X    作者:taers232c    | 项目源码 | 文件源码
def parse(xml_string, target_class=None, version=1, encoding=None):
  """Parses the XML string according to the rules for the target_class.

  Args:
    xml_string: str or unicode
    target_class: XmlElement or a subclass. If None is specified, the
        XmlElement class is used.
    version: int (optional) The version of the schema which should be used when
        converting the XML into an object. The default is 1.
    encoding: str (optional) The character encoding of the bytes in the
        xml_string. Default is 'UTF-8'.
  """
  if target_class is None:
    target_class = XmlElement
  if isinstance(xml_string, unicode):
    if encoding is None:
      xml_string = xml_string.encode(STRING_ENCODING)
    else:
      xml_string = xml_string.encode(encoding)
  tree = ElementTree.fromstring(xml_string)
  return _xml_element_from_tree(tree, target_class, version)
项目:intel_collection_tools    作者:wolfpack1    | 项目源码 | 文件源码
def domain_categorize(url,webCats):
    hostname = url
    port = '80'
    r = urllib2.urlopen('http://sp.cwfservice.net/1/R/%s/K9-00006/0/GET/HTTP/%s/%s///' % (k9License, hostname, port))
    if r.code == 200:       
        e = fromstring(r.read())
        domc = e.find('DomC')
        dirc = e.find('DirC')
        if domc is not None:
            cats = _chunks(domc.text)
            #domain_cat = '%s,%s' % (hostname, [webCats.get(c.lower(), 'Unknown') for c in cats][0])
            domain_cat = '%s' % [webCats.get(c.lower(), 'Unknown') for c in cats][0]
        elif dirc is not None:
            cats = _chunks(dirc.text)
            #domain_cat =  '%s,%s' % (hostname, [webCats.get(c.lower(), 'Unknown') for c in cats][0])
            domain_cat =  '%s' % [webCats.get(c.lower(), 'Unknown') for c in cats][0]
    else:
        print 'Cannot get category for %s\n' % hostname
        domain_cat = '404'
    return domain_cat
    #exit(0)
项目:docker-zenoss4    作者:krull    | 项目源码 | 文件源码
def send_request(self, request_template_name, **kwargs):
        resp = yield self._sender.send_request(
            request_template_name, **kwargs)
        proto = _StringProtocol()
        resp.deliverBody(proto)
        body = yield proto.d
        if self._sender.is_kerberos():
            xml_str = self._sender.gssclient.decrypt_body(body)
        else:
            xml_str = yield body
        if log.isEnabledFor(logging.DEBUG):
            try:
                import xml.dom.minidom
                xml = xml.dom.minidom.parseString(xml_str)
                log.debug(xml.toprettyxml())
            except:
                log.debug('Could not prettify response XML: "{0}"'.format(xml_str))
        defer.returnValue(ET.fromstring(xml_str))
项目:watcher    作者:nosmokingbandit    | 项目源码 | 文件源码
def parse_newznab_xml(self, feed):
        ''' Parse xml from Newnzb api.
        :param feed: str nn xml feed
        kind: str type of feed we are parsing, either nzb or torrent

        Returns dict of sorted nzb information.
        '''

        root = ET.fromstring(feed)
        indexer = u''
        # This is so ugly, but some newznab sites don't output json. I don't want to include a huge xml parsing module, so here we are. I'm not happy about it either.
        res_list = []
        for root_child in root:
            if root_child.tag == u'channel':
                for channel_child in root_child:
                    if channel_child.tag == u'title':
                        indexer = channel_child.text
                    if not indexer and channel_child.tag == u'link':
                        indexer = channel_child.text
                    if channel_child.tag == u'item':
                        result_item = self._make_item_dict(channel_child)
                        result_item['indexer'] = indexer
                        res_list.append(result_item)
        return res_list
项目:watcher    作者:nosmokingbandit    | 项目源码 | 文件源码
def parse_predb_xml(self, feed):
        ''' Helper function to parse predb xmlrpclib
        :param feed: str rss feed

        Returns list of items with 'title' in tag
        '''

        root = ET.fromstring(feed)

        # This so ugly, but some newznab sites don't output json.
        items = []
        for item in root.iter('item'):
            for i_c in item:
                if i_c.tag == u'title':
                    items.append(i_c.text)
        return items

    # keeps checking release titles until one matches or all are checked
项目:watcher    作者:nosmokingbandit    | 项目源码 | 文件源码
def parse_xml(self, feed):
        ''' Turns rss into python dict
        :param feed: str rss feed

        Returns list of dicts of movies in rss
        '''

        root = ET.fromstring(feed)

        # This so ugly, but some newznab sites don't output json.
        items = []
        for item in root.iter('item'):
            d = {}
            for i_c in item:
                d[i_c.tag] = i_c.text
            items.append(d)
        return items
项目:enigma2-plugins    作者:opendreambox    | 项目源码 | 文件源码
def gotPage(self, data, id = None):
        feed = cElementTree_fromstring(data)

        # For Single-Polling
        if id is not None:
            self.feeds[id].gotFeed(feed)
            print("[SimpleRSS] single feed parsed...")
            return

        new_items = self.feeds[self.current_feed].gotFeed(feed)

        print("[SimpleRSS] feed parsed...")

        # Append new items to locally bound ones
        if new_items is not None:
            self.newItemFeed.history.extend(new_items)

        # Start Timer so we can either fetch next feed or show new_items
        self.next_feed()
项目:whatstyle    作者:mikr    | 项目源码 | 文件源码
def tree_from_html(htmldata, enc='utf-8'):
    # type: (str, str) -> ETree.Element
    text = htmldata
    text = escape_illegal_xmlchars(text)
    if NARROW_BUILD:
        # Remove lonely surrogate halfs
        text = u''.join(iterchars(text))
    text = re.sub(' xmlns="[^"]+"', '', text, count=1)
    text = text.replace('&nbsp;', ' ')
    btext = bytestr('<?xml version="1.0" encoding="%s"?>\n' % enc) + surrencode(text, enc)
    tree = ETree.fromstring(btext)
    return tree

# The LINENRSEP must not be anything that appears in the line number column of the HTML
# generated by difflib which are digits and the line continuation character '>'.
项目:otRebuilder    作者:Pal3love    | 项目源码 | 文件源码
def loads(string):
    """Load designspace from a string."""
    return _load(ET.fromstring(string))
项目:otRebuilder    作者:Pal3love    | 项目源码 | 文件源码
def _glifTreeFromString(aString):
    root = ElementTree.fromstring(aString)
    if root.tag != "glyph":
        raise GlifLibError("The GLIF is not properly formatted.")
    if root.text and root.text.strip() != '':
        raise GlifLibError("Invalid GLIF structure.")
    return root
项目:code    作者:ActiveState    | 项目源码 | 文件源码
def __new__(cls,tag,thing = None,*args,**kwargs):
    if hasattr(tag,'__xml__'):
      return tag.__xml__()
    self = object.__new__(xml)
    if cElementTree.iselement(tag):
      self.__content = tag
    elif isinstance(tag,cElementTree.ElementTree):
      self.__content = tag.getroot()
    elif is_file(tag):
      self.__content = cElementTree.parse(tag).getroot()
    elif isinstance(tag,str) and len(tag) > 0 and tag[0] == '<':
      self.__content = cElementTree.fromstring(tag)
    else:
      if type(tag) != str:
        raise TypeError("Cannot convert %s object to xml" % str(type(tag)))
      self.__content = cElementTree.fromstring('<%s/>' % tag)
      if is_text(thing) or type(thing) == int:
        self.__content.text = text(thing)
      elif thing != None:
        self.append(xml(thing))
      for subthing in args:
        self.append(xml(subthing))
      for key,value in kwargs.items():
        if key == '__class' or key == 'klass':
          self['class'] = value
        else:
          self[key] = value
    if '{' in self.__content.tag:
      self.__prefix = PREFIX_PAT.search(self.__content.tag).groups()[0]
    else:
      self.__prefix = ''
    return self