脚本专栏 
首页 > 脚本专栏 > 浏览文章

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

(编辑:jimmy 日期: 2024/5/9 浏览:3 次 )

总结了一下使用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 单例设计模式用法实例分析
下一篇:详解Python3定时器任务代码
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网