Python3使用xml.dom.minidom和xml.etree模块儿解析xml文件封装函数的方法

 更新时间:2019年09月23日 11:04:42   作者:撒欢  
这篇文章主要介绍了Python3使用xml.dom.minidom和xml.etree模块儿解析xml文件,封装函数的方法,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

总结了一下使用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文件封装函数的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

  • Python flask-script 模块详解

    Python flask-script 模块详解

    Flask Script扩展提供向Flask插入外部脚本的功能,这篇文章主要介绍了Flask之flask-script模块使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-11-11
  • 使用python实现md5加密

    使用python实现md5加密

    这篇文章主要介绍了使用python实现md5加密,md5为计算机安全领域广泛使用的一种散列函数,用于确保信息传输完整一致,需要的朋友可以参考下
    2023-04-04
  • Python使用内存缓存实例分享

    Python使用内存缓存实例分享

    Python中的内存缓存是一种将计算结果存储在内存中,以便在后续调用时快速获取结果的技术。通过使用装饰器和字典等数据结构,可以轻松实现内存缓存功能,提高程序的执行效率。
    2023-09-09
  • python使用beautifulsoup4爬取酷狗音乐代码实例

    python使用beautifulsoup4爬取酷狗音乐代码实例

    这篇文章主要介绍了python使用beautifulsoup4爬取酷狗音乐代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12
  • Python 读写文件和file对象的方法(推荐)

    Python 读写文件和file对象的方法(推荐)

    下面小编就为大家带来一篇Python 读写文件和file对象的方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-09-09
  • Python中实现对Timestamp和Datetime及UTC时间之间的转换

    Python中实现对Timestamp和Datetime及UTC时间之间的转换

    这篇文章主要介绍了Python中实现对Timestamp和Datetime及UTC时间之间的转换,例子则主要针对Ubuntu等类UNIX系统,需要的朋友可以参考下
    2015-04-04
  • Linux环境下的Python打包和部署实践

    Linux环境下的Python打包和部署实践

    这篇文章主要介绍了Linux环境下的Python打包和部署实践的相关资料,需要的朋友可以参考下
    2023-11-11
  • 探究数组排序提升Python程序的循环的运行效率的原因

    探究数组排序提升Python程序的循环的运行效率的原因

    这篇文章主要介绍了探究数组排序提升Python程序的循环的运行效率的原因,作者用代码实践了多个小片段来进行对比解释,需要的朋友可以参考下
    2015-04-04
  • pytorch 常用函数 max ,eq说明

    pytorch 常用函数 max ,eq说明

    这篇文章主要介绍了pytorch 常用函数 max eq说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-06-06
  • Python 网络编程起步(Socket发送消息)

    Python 网络编程起步(Socket发送消息)

    现在开始学习网络编程,先从简单的UDP协议发送消息开始。我们需要有接受消息的服务端程序(Server.py)和发送消息的客户端程序(Client)。
    2008-09-09

最新评论