Python xmltodict.parse() Examples
The following are 30
code examples of xmltodict.parse().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
xmltodict
, or try the search function
.
Example #1
Source File: getlogs_evtx.py From InsightAgent with Apache License 2.0 | 7 votes |
def get_timestamp_from_date_string(date_string): """ parse a date string into unix epoch (ms) """ if 'strip_tz' in agent_config_vars and agent_config_vars['strip_tz']: date_string = ''.join(agent_config_vars['strip_tz_fmt'].split(date_string)) if 'timestamp_format' in agent_config_vars: if agent_config_vars['timestamp_format'] == 'epoch': timestamp_datetime = get_datetime_from_unix_epoch(date_string) else: timestamp_datetime = datetime.strptime(date_string, agent_config_vars['timestamp_format']) else: try: timestamp_datetime = dateutil.parse.parse(date_string) except: timestamp_datetime = get_datetime_from_unix_epoch(date_string) agent_config_vars['timestamp_format'] = 'epoch' return get_timestamp_from_datetime(timestamp_datetime)
Example #2
Source File: component.py From wechatpy with MIT License | 7 votes |
def parse_message(self, msg, msg_signature, timestamp, nonce): """ 处理 wechat server 推送消息 :params msg: 加密内容 :params msg_signature: 消息签名 :params timestamp: 时间戳 :params nonce: 随机数 """ content = self.crypto.decrypt_message(msg, msg_signature, timestamp, nonce) message = xmltodict.parse(to_text(content))["xml"] message_type = message["InfoType"].lower() message_class = COMPONENT_MESSAGE_TYPES.get(message_type, ComponentUnknownMessage) msg = message_class(message) if msg.type == "component_verify_ticket": self.session.set(msg.type, msg.verify_ticket) elif msg.type in ("authorized", "updateauthorized"): msg.query_auth_result = self.query_auth(msg.authorization_code) return msg
Example #3
Source File: bulk.py From tap-salesforce with GNU Affero General Public License v3.0 | 6 votes |
def _add_batch(self, catalog_entry, job_id, start_date, order_by_clause=True): endpoint = "job/{}/batch".format(job_id) url = self.bulk_url.format(self.sf.instance_url, endpoint) body = self.sf._build_query_string(catalog_entry, start_date, order_by_clause=order_by_clause) headers = self._get_bulk_headers() headers['Content-Type'] = 'text/csv' with metrics.http_request_timer("add_batch") as timer: timer.tags['sobject'] = catalog_entry['stream'] resp = self.sf._make_request('POST', url, headers=headers, body=body) batch = xmltodict.parse(resp.text) return batch['batchInfo']['id']
Example #4
Source File: nmap.py From CrackMapExec with BSD 2-Clause "Simplified" License | 6 votes |
def parse_nmap_xml(nmap_output_file, protocol): targets = [] with open(nmap_output_file, 'r') as file_handle: scan_output = xmltodict.parse(file_handle.read()) for host in scan_output['nmaprun']['host']: if host['address'][0]['@addrtype'] != 'ipv4': continue ip = host['address'][0]['@addr'] for port in host['ports']['port']: if port['state']['@state'] == 'open': if 'service' in port and (port['service']['@name'] in protocol_dict[protocol]['services']): if ip not in targets: targets.append(ip) elif port['@portid'] in protocol_dict[protocol]['ports']: if ip not in targets: targets.append(ip) return targets
Example #5
Source File: reader.py From chimera with MIT License | 6 votes |
def __init__(self, file_name): self.data = [] content = open(file_name, encoding="utf-8").read() is_test_file = file_name.split("/")[-1] == "testdata_with_lex.xml" structure = xmltodict.parse(content) for i, entry in enumerate(structure["benchmark"]["entries"]["entry"]): triplets = [tuple(map(str.strip, r.split("|"))) for r in self.triplets_from_object(entry["modifiedtripleset"], "mtriple")] sentences = list(self.extract_sentences(entry["lex"])) for s in sentences: info = { "id": i, "seen": not is_test_file or i <= 970, "manual": is_test_file and i + 1 in FOR_MANUAL_EVAL and i <= 970 } self.data.append(Datum(rdfs=triplets, text=s, info=info))
Example #6
Source File: __init__.py From wechatpy with MIT License | 6 votes |
def _handle_result(self, res): res.encoding = "utf-8-sig" xml = res.text logger.debug("Response from WeChat API \n %s", xml) try: data = xmltodict.parse(xml)["xml"] except (xmltodict.ParsingInterrupted, ExpatError): # 解析 XML 失败 logger.debug("WeChat payment result xml parsing error", exc_info=True) return xml return_code = data["return_code"] return_msg = data.get("return_msg", data.get("retmsg")) result_code = data.get("result_code", data.get("retcode")) errcode = data.get("err_code") errmsg = data.get("err_code_des") if return_code != "SUCCESS" or result_code != "SUCCESS": # 返回状态码不为成功 raise WeChatPayException( return_code, result_code, return_msg, errcode, errmsg, client=self, request=res.request, response=res, ) return data
Example #7
Source File: upnpc.py From p2p-python with MIT License | 6 votes |
def get_external_ip(soap_url) -> str: """get external ip address""" s_o_a_p = '<?xml version="1.0"?>\r\n' s_o_a_p += '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle=' \ '"http://schemas.xmlsoap.org/soap/encoding/">\r\n' s_o_a_p += '<s:Body>\r\n' s_o_a_p += '<u:GetExternalIPAddress xmlns:u="urn:schemas-upnp-org:service:WANPPPConnection:1">\r\n' s_o_a_p += '</u:GetExternalIPAddress>\r\n' s_o_a_p += '</s:Body>\r\n' s_o_a_p += '</s:Envelope>\r\n' try: req = Request(soap_url) req.add_header('Content-Type', 'text/xml; charset="utf-8"') req.add_header('SOAPACTION', '"urn:schemas-upnp-org:service:WANPPPConnection:1#GetExternalIPAddress"') req.data = s_o_a_p.encode('utf8') result = xmltodict.parse(urlopen(req).read().decode()) return result['s:Envelope']['s:Body']['u:GetExternalIPAddressResponse']['NewExternalIPAddress'] except Exception: log.debug("get_external_ip exception", exc_info=True)
Example #8
Source File: runpytornado.py From CEASIOMpy with Apache License 2.0 | 6 votes |
def get_pytornado_settings_from_CPACS(cpacs_in_path): """ Try to read PyTornado settings from CPACS Note: * Returns None if PyTornado settings not found in CPACS Args: cpacs_in_path (str): Path to CPACS file Returns: cpacs_settings (dict): PyTornado settings dictionary read from CPACS """ with open(cpacs_in_path, "r") as fp: cpacs_as_dict = xml.parse(fp.read()) cpacs_settings = cpacs_as_dict.get('cpacs', {}).get('toolspecific', {}).get('pytornado', None) parse_pytornado_settings_dict(cpacs_settings) return cpacs_settings
Example #9
Source File: example_tools.py From buzzard with Apache License 2.0 | 6 votes |
def _url_status(url): parse_obj = urllib.parse.urlparse(url) timer = 1 for i in range(6): try: connection = http.client.HTTPConnection(parse_obj.netloc) connection.request('HEAD', parse_obj.path) break except Exception as e: print(url, e, 'sleep', timer) time.sleep(timer) timer *= 2 else: return e response = connection.getresponse() connection.close() return response.status
Example #10
Source File: QoS.py From catalyst9k-network-automation with BSD 3-Clause "New" or "Revised" License | 6 votes |
def get_qos_policy_map(netconf_handler): ''' This procedure takes in the netconf handler for the switch and read what is the QoS Softmax Multiplier Procedure returns True if configuration successful, else returns False ''' netconf_reply = netconf_handler.get_config( source='running', filter=('xpath', "/native/policy/policy-map")) data = xmltodict.parse(netconf_reply.xml) policy_map_names = [] for pmap in (data["rpc-reply"]["data"]["native"]["policy"]["policy-map"]): if (pmap["name"]) != "system-cpp-policy": class_maps_in_policy = " Contains Class maps: " for cmap in (pmap["class"]): class_maps_in_policy = class_maps_in_policy + cmap["name"] + " " policy_map_names.append("Policy map: " + pmap["name"] + class_maps_in_policy) return policy_map_names
Example #11
Source File: QoS.py From catalyst9k-network-automation with BSD 3-Clause "New" or "Revised" License | 6 votes |
def get_qos_class_map(netconf_handler): ''' This procedure takes in the netconf handler for the switch and read what is the QoS Softmax Multiplier Procedure returns True if configuration successful, else returns False ''' netconf_reply = netconf_handler.get_config( source='running', filter=('xpath', "/native/policy/class-map")) data = xmltodict.parse(netconf_reply.xml) #print (data) class_map_names = [] for cmap in (data["rpc-reply"]["data"]["native"]["policy"]["class-map"]): class_map_names.append(cmap["name"]) return class_map_names
Example #12
Source File: read_epub.py From Lector with GNU General Public License v3.0 | 6 votes |
def generate_references(self): self.zip_file = zipfile.ZipFile( self.book_filename, mode='r', allowZip64=True) self.file_list = self.zip_file.namelist() # Book structure relies on parsing the .opf file # in the book. Now that might be the usual content.opf # or package.opf or it might be named after your favorite # eldritch abomination. The point is we have to check # the container.xml container = self.find_file('container.xml') if container: container_xml = self.zip_file.read(container) container_dict = xmltodict.parse(container_xml) packagefile = container_dict['container']['rootfiles']['rootfile']['@full-path'] else: presumptive_names = ('content.opf', 'package.opf', 'volume.opf') for i in presumptive_names: packagefile = self.find_file(i) if packagefile: logger.info('Using presumptive package file: ' + self.book_filename) break packagefile_data = self.zip_file.read(packagefile) self.opf_dict = xmltodict.parse(packagefile_data)
Example #13
Source File: read_epub.py From Lector with GNU General Public License v3.0 | 6 votes |
def get_chapter_content(self, chapter_file): this_file = self.find_file(chapter_file) if this_file: chapter_content = self.zip_file.read(this_file).decode() # Generate a None return for a blank chapter # These will be removed from the contents later contentDocument = QtGui.QTextDocument(None) contentDocument.setHtml(chapter_content) contentText = contentDocument.toPlainText().replace('\n', '') if contentText == '': chapter_content = None return chapter_content else: return 'Possible parse error: ' + chapter_file
Example #14
Source File: client.py From rtcclient with Apache License 2.0 | 6 votes |
def _createWorkitem(self, url_post, workitem_raw): headers = copy.deepcopy(self.headers) headers['Content-Type'] = self.OSLC_CR_XML resp = self.post(url_post, verify=False, headers=headers, proxies=self.proxies, data=workitem_raw) raw_data = xmltodict.parse(resp.content) workitem_raw = raw_data["oslc_cm:ChangeRequest"] workitem_id = workitem_raw["dc:identifier"] workitem_url = "/".join([self.url, "oslc/workitems/%s" % workitem_id]) new_wi = Workitem(workitem_url, self, workitem_id=workitem_id, raw_data=raw_data["oslc_cm:ChangeRequest"]) self.log.info("Successfully create <Workitem %s>" % new_wi) return new_wi
Example #15
Source File: repository.py From margaritashotgun with MIT License | 5 votes |
def parse_manifest(self, manifest_xml): """ Parse manifest xml file :type manifest_xml: str :param manifest_xml: raw xml content of manifest file """ manifest = dict() try: mdata = xmltodict.parse(manifest_xml)['modules']['module'] for module in mdata: mod = dict() mod['type'] = module['@type'] mod['name'] = module['name'] mod['arch'] = module['arch'] mod['checksum'] = module['checksum'] mod['version'] = module['version'] mod['packager'] = module['packager'] mod['location'] = module['location']['@href'] mod['signature'] = module['signature']['@href'] mod['platform'] = module['platform'] manifest[mod['version']] = mod except Exception as e: raise return manifest
Example #16
Source File: repository.py From margaritashotgun with MIT License | 5 votes |
def parse_metadata(self, metadata_xml): """ Parse repomd.xml file :type metadata_xml: str :param metadata_xml: raw xml representation of repomd.xml """ try: metadata = dict() mdata = xmltodict.parse(metadata_xml)['metadata'] metadata['revision'] = mdata['revision'] metadata['manifests'] = dict() # check if multiple manifests are present if type(mdata['data']) is list: manifests = mdata['data'] else: manifests = [mdata['data']] for manifest in manifests: manifest_dict = dict() manifest_dict['type'] = manifest['@type'] manifest_dict['checksum'] = manifest['checksum'] manifest_dict['open_checksum'] = manifest['open_checksum'] manifest_dict['location'] = manifest['location']['@href'] manifest_dict['timestamp'] = datetime.fromtimestamp( int(manifest['timestamp'])) manifest_dict['size'] = int(manifest['size']) manifest_dict['open_size'] = int(manifest['open_size']) metadata['manifests'][manifest['@type']] = manifest_dict except Exception as e: raise RepositoryError("{0}/{1}".format(self.url,self.metadata_dir, self.metadata_file), e) return metadata
Example #17
Source File: utils.py From structured-neural-summarization with MIT License | 5 votes |
def load_xml(filename: str, func: Callable, depth: int) -> Any: with open(filename, 'rb') as f: return xmltodict.parse(f, item_depth=depth, item_callback=func)
Example #18
Source File: mwechat.py From rasa_wechat with Apache License 2.0 | 5 votes |
def post(self, request): args = self._get_args(request) weixin = WeixinMpAPI(**args) if not weixin.validate_signature(): raise AttributeError("Invalid weixin signature") xml_str = self._get_xml(request.body) crypt = WXBizMsgCrypt(token, encoding_aeskey, appid) decryp_xml, nonce = self._decrypt_xml(request.raw_args, crypt, xml_str) xml_dict = xmltodict.parse(decryp_xml) xml = WXResponse(xml_dict)() or 'success' encryp_xml = self._encryp_xml(crypt, xml, nonce) return text(encryp_xml or xml)
Example #19
Source File: example.py From network-programmability-stream with MIT License | 5 votes |
def get_config(nc_conn): nc_reply = nc_conn.get_config(source='running') current_config = xmltodict.parse(nc_reply.data_xml)['data'] # print(json.dumps(current_config, indent=2)) sw_version = current_config['native']['version'] hostname = current_config['native']['hostname'] print(f'SW version: {sw_version}') print(f'hostname: {hostname}')
Example #20
Source File: xml_to_dict.py From python-tools with MIT License | 5 votes |
def xml_to_dict(xml): """Convert xml to dict. Args: xml (str): XML. Returns: dict: Return a dict representation of an XML. """ try: my_dict = xmltodict.parse(xml) except Exception: my_dict = {} return json.loads(json.dumps(my_dict))
Example #21
Source File: test_flask_aaa.py From minemeld-core with Apache License 2.0 | 5 votes |
def _num_collections(self, resp): ans = xmltodict.parse(resp.data) collections = ans['taxii_11:Collection_Information_Response']['taxii_11:Collection'] if isinstance(collections, list): return len(collections) return 1
Example #22
Source File: nessus.py From CrackMapExec with BSD 2-Clause "Simplified" License | 5 votes |
def parse_nessus_file(nessus_file, protocol): targets = [] def handle_nessus_file(path, item): # Must return True otherwise xmltodict will throw a ParsingIterrupted() exception # https://github.com/martinblech/xmltodict/blob/master/xmltodict.py#L219 if any('ReportHost' and 'ReportItem' in values for values in path): item = dict(path) ip = item['ReportHost']['name'] if ip in targets: return True port = item['ReportItem']['port'] svc_name = item['ReportItem']['svc_name'] if port in protocol_dict[protocol]['ports']: targets.append(ip) if svc_name in protocol_dict[protocol]['services']: targets.append(ip) return True else: return True with open(nessus_file, 'r') as file_handle: xmltodict.parse(file_handle, item_depth=4, item_callback=handle_nessus_file) return targets
Example #23
Source File: __init__.py From wechatpy with MIT License | 5 votes |
def _fetch_sandbox_api_key(self): nonce_str = random_string(32) sign = calculate_signature({"mch_id": self.mch_id, "nonce_str": nonce_str}, self.api_key) payload = dict_to_xml({"mch_id": self.mch_id, "nonce_str": nonce_str,}, sign=sign) headers = {"Content-Type": "text/xml"} api_url = f"{self.API_BASE_URL}sandboxnew/pay/getsignkey" response = self._http.post(api_url, data=payload, headers=headers) return xmltodict.parse(response.text)["xml"].get("sandbox_signkey")
Example #24
Source File: QoS.py From catalyst9k-network-automation with BSD 3-Clause "New" or "Revised" License | 5 votes |
def get_qos_softmax(netconf_handler): ''' This procedure takes in the netconf handler for the switch and read what is the QoS Softmax Multiplier Procedure returns True if configuration successful, else returns False ''' netconf_reply = netconf_handler.get_config( source='running', filter=('xpath', "/native/qos/queue-softmax-multiplier/value")) data = xmltodict.parse(netconf_reply.xml) return (data["rpc-reply"]["data"]["native"]["qos"]["queue-softmax-multiplier"]["value"])
Example #25
Source File: test_template.py From rtcclient with Apache License 2.0 | 5 votes |
def test_get_template(self, mytemplater, mocker): # invalid template names invalid_names = [None, True, False, "", u"", 123.4] for invalid_name in invalid_names: with pytest.raises(BadValue): mytemplater.getTemplate(invalid_name, template_name=None, template_folder=None, keep=False, encoding="UTF-8") # valid template name mocked_get = mocker.patch("requests.get") mock_resp = mocker.MagicMock(spec=requests.Response) mock_resp.status_code = 200 mock_resp.content = utils_test.workitem1_raw mocked_get.return_value = mock_resp copied_from_valid_names = [161, "161", u"161"] for copied_from in copied_from_valid_names: template_161 = mytemplater.getTemplate(copied_from, template_name=None, template_folder=None, keep=False, encoding="UTF-8") assert (list(xmltodict.parse(template_161).items()).sort() == list(utils_test.template_ordereddict.items()).sort())
Example #26
Source File: workitem.py From rtcclient with Apache License 2.0 | 5 votes |
def addAttachment(self, filepath): """Upload attachment to a workitem :param filepath: the attachment file path :return: the :class:`rtcclient.models.Attachment` object :rtype: rtcclient.models.Attachment """ proj_id = self.contextId fa = self.rtc_obj.getFiledAgainst(self.filedAgainst, projectarea_id=proj_id) fa_id = fa.url.split("/")[-1] headers = copy.deepcopy(self.rtc_obj.headers) if headers.__contains__("Content-Type"): headers.__delitem__("Content-Type") filename = os.path.basename(filepath) fileh = open(filepath, "rb") files = {"attach": (filename, fileh, "application/octet-stream")} params = {"projectId": proj_id, "multiple": "true", "category": fa_id} req_url = "".join([self.rtc_obj.url, "/service/com.ibm.team.workitem.service.", "internal.rest.IAttachmentRestService/"]) resp = self.post(req_url, verify=False, headers=headers, proxies=self.rtc_obj.proxies, params=params, files=files) raw_data = xmltodict.parse(resp.content) json_body = json.loads(raw_data["html"]["body"]["textarea"]) attachment_info = json_body["files"][0] return self._add_attachment_link(attachment_info)
Example #27
Source File: workitem.py From rtcclient with Apache License 2.0 | 5 votes |
def _perform_subscribe(self): subscribers_url = "".join([self.url, "?oslc_cm.properties=rtc_cm:subscribers"]) headers = copy.deepcopy(self.rtc_obj.headers) headers["Content-Type"] = self.OSLC_CR_RDF headers["Accept"] = self.OSLC_CR_RDF headers["OSLC-Core-Version"] = "2.0" resp = self.get(subscribers_url, verify=False, proxies=self.rtc_obj.proxies, headers=headers) headers["If-Match"] = resp.headers.get("etag") raw_data = xmltodict.parse(resp.content) return headers, raw_data
Example #28
Source File: wildfire.py From python-sandboxapi with GNU General Public License v2.0 | 5 votes |
def decode(self, response): """Convert a xml response to a python dictionary. :param requests.Response response: A Response object with xml content. :rtype: dict :return: The xml content converted to a dictionary. """ # This weird conversion to and from JSON is because the XML is being parsed as an Ordereddict. # TODO: See if there's a better way to do this without having to convert to JSON. output = json.loads(json.dumps(xmltodict.parse(response.content.decode('utf-8')))) if 'error' in output: raise sandboxapi.SandboxError(output['error']['error-message']) return output
Example #29
Source File: template.py From rtcclient with Apache License 2.0 | 5 votes |
def listFieldsFromSource(self, template_source): """List all the attributes to be rendered directly from template source :param template_source: the template source (usually represents the template content in string format) :return: a :class:`set` contains all the needed attributes :rtype: set """ ast = self.environment.parse(template_source) return jinja2.meta.find_undeclared_variables(ast)
Example #30
Source File: reader.py From chimera with MIT License | 5 votes |
def __init__(self, file_name): self.data = [] content = open(file_name, encoding="utf-8").read() structure = xmltodict.parse(content) for entry in structure["benchmark"]["entries"]["entry"]: triplets = [tuple(map(str.strip, r.split("|"))) for r in self.triplets_from_object(entry["modifiedtripleset"], "mtriple")] sentences = list(self.extract_sentences(entry["lex"])) for text, delex in sentences: self.data.append(Datum(rdfs=triplets, text=text, delex=delex))