python etree模块所有函数详解_Python3使用xml.dom.minidom和xml.etree模块儿解析xml文件,封装函数...

from xml.dom.minidom importparse, parseStringfrom xml.etree importElementTreeimportxml.dom.minidom"""Get XML String info 查询属性值

response:xml string

tag:xml tag

element:xml attribute"""

defget_xml_info(response, element):

DOMTree=xml.dom.minidom.parseString(response)returnDOMTree.documentElement.getAttribute(element)"""Get XML String info 查询制定名称的特定标签id

xmlstring:xml str

return config id"""

defget_config_id_from_xml(xmlstring, scan):

root=ElementTree.fromstring(xmlstring)

configs= root.findall('config')for config inconfigs:

config_name= config.find('name').textif config_name ==scan:return config.attrib['id']"""Get XML String info 查询指定id

xmlstring:xml str

return report id"""

defget_report_id_from_xml(xmlstring):

root=ElementTree.fromstring(xmlstring)

report_id= root.find('report_id').textreturnreport_id"""Get XML String info

xmlstring:xml str

return progress"""

defget_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:returnprogress"""Get XML Report info 从xml文件查询

file_path : report path"""

defget_xml_report(file_path):

report={}

result_dicts={}

resultsList=[]try:

root=ElementTree.parse(file_path)except:return{}if root is notNone:

creation_time= root.find("creation_time")if creation_time is notNone:

report[creation_time.tag]=creation_time.textif root.find("report") is notNone:

scan_start= root.find("report").find("scan_start")if scan_start is notNone:ifscan_start.text:

report[scan_start.tag]=scan_start.text

results= root.getiterator("result")if results is notNone:for result inresults:if result.find("threat") is notNone:if result.find("threat").text != "Log":

resultsList.append(getResults(result))

report["Results"] =resultsListreturn report

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值