您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)怎么在Python3中使用xml.dom.minidom和xml.etree模塊解析xml文件,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
from xml.dom.minidom import parse, parseString from xml.etree import ElementTree import xml.dom.minidom """ Get XML String info 查詢屬性值 response:xml string tag:xml tag element:xml attribute """ def get_xml_info(response, element): DOMTree = xml.dom.minidom.parseString(response) return DOMTree.documentElement.getAttribute(element) """ Get XML String info 查詢制定名稱的特定標(biāo)簽id xmlstring:xml str return config id """ def get_config_id_from_xml(xmlstring, scan): root = ElementTree.fromstring(xmlstring) configs = root.findall('config') for config in configs: config_name = config.find('name').text if config_name == scan: return config.attrib['id'] """ Get XML String info 查詢指定id xmlstring:xml str return report id """ def get_report_id_from_xml(xmlstring): root = ElementTree.fromstring(xmlstring) report_id = root.find('report_id').text return report_id """ Get XML String info xmlstring:xml str return progress """ def get_progress_from_xml(xmlstring): root = ElementTree.fromstring(xmlstring) task = root.find('task') progress = float(task.find('progress').text) if progress < 0: return 100.0 else: return progress """ Get XML Report info 從xml文件查詢 file_path : report path """ def get_xml_report(file_path): report = {} result_dicts = {} resultsList = [] try: root = ElementTree.parse(file_path) except: return {} if root is not None: creation_time = root.find("creation_time") if creation_time is not None: report[creation_time.tag] = creation_time.text if root.find("report") is not None: scan_start = root.find("report").find("scan_start") if scan_start is not None: if scan_start.text: report[scan_start.tag] = scan_start.text results = root.getiterator("result") if results is not None: for result in results: if result.find("threat") is not None: if result.find("threat").text != "Log": resultsList.append(getResults(result)) report["Results"] = resultsList return report
以上就是怎么在Python3中使用xml.dom.minidom和xml.etree模塊解析xml文件,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。