总结了一下使用Python对xml文件的解析,用到的模块儿如下:
分别从xml字符串和xml文件转换为xml对象,然后解析xml内容,查询指定信息字段。
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 查询制定名称的特定标签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文件封装函数的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
风云阁资源网 Design By www.bgabc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
风云阁资源网 Design By www.bgabc.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 黑鸭子.2005-《发烧DSD》柏菲[WAV分轨]
- 群星.1990-电影看板·人间道【宝丽金】【WAV+CUE】
- 周殷廷.2024-TO.WHOM.IT.MAY.CONCERN【环球】【FLAC分轨】
- 草蜢.1997-三人世界精选17首【宝丽金】【WAV+CUE】
- 【古琴音乐】管平湖《广陵散》2004[FLAC+CUE]
- 【中国民乐】《少数民族器乐·苗岭的早晨》2004[WAV+CUE]
- 【中国音乐】盛中国《新疆之春》2004[FLAC+CUE]
- 群星《音你而来 第8期》[320K/MP3][44.64MB]
- 群星《音你而来 第8期》[FLAC/分轨][121.21MB]
- 群星《十二女声Ⅲ》[WAV/CUE/分轨][626.4MB]
- 陈百强.1993-离不开...纪念歌集2CD【华纳】【WAV+CUE】
- 于台烟.2003-爱自己【乾坤唱片】【WAV+CUE】
- 群星.2024-我们永远是我们电影原声带【乐禾发文化】【FLAC分轨】
- 【中国民乐】张维良《秋江夜泊》2004[FLAC+CUE]
- 明达年度发烧碟MasterSuperiorAudiophile2011[DSF]