总结了一下使用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文件,封装函数的更多相关文章

  1. xml dom minidom

    一. xml相关术语: 1.Document(文档): 对应一个xml文件 2.Declaration(声明): <?xml version="1.0" encoding=& ...

  2. python XML文件解析:用xml.dom.minidom来解析xml文件

    python解析XML常见的有三种方法: 一是xml.dom.*模块,是W3C DOM API的实现,若需要处理DOM API则该模块很合适, 二是xml.sax.*模块,它是SAX API的实现,这 ...

  3. python 应用xml.dom.minidom读xml

    xml文件 <?xml version="1.0" encoding="utf-8"?> <city> <name>上海&l ...

  4. python模块:xml.dom.minidom

    """Simple implementation of the Level 1 DOM. Namespaces and other minor Level 2 featu ...

  5. python 之模块之 xml.dom.minidom解析xml

    # -*- coding: cp936 -*- #python 27 #xiaodeng #python 之模块之 xml.dom.minidom解析xml #http://www.cnblogs.c ...

  6. python-minidom模块【解析xml】

    1,xml的文档结构 1.1,XML文档包括XML头信息和XML信息体 1.1.1,XML文档头信息 <?xml version="1.0" encoding="u ...

  7. nodejs模块xml2js解析xml的坑

    在一个项目中,用到nodejs模块xml2js解析xml,xml的数据如下: <xml> <MsgId>6197906553041859764</MsgId> &l ...

  8. [java开发篇][dom模块] 遍历解析xml

    http://blog.csdn.net/andie_guo/article/details/24844351 XML DOM节点树 XML DOM将XML文档作为树结构,树结构称为一个节点树.所有的 ...

  9. java解析xml汇总(转自倾城幻影-Java解析xml汇总,链接:http://www.cnblogs.com/jiugehuanying/archive/2012/01/12/2320058.html)

    [引言] 目前在Java中用于解析XML的技术很多,主流的有DOM.SAX.JDOM.DOM4j,下文主要介绍这4种解析XML文档技术的使用.优缺点及性能测试. [一.基础知识--扫盲] sax.do ...

随机推荐

  1. JMeter学习1

    Jmeter的组织方式相对比较扁平,直接是TestPlan(相当于Project),TestPlan下创建的ThreadsGroup(相当于TestCase), Jmeter一个TestPlan也是一 ...

  2. 【UVA11134】传说中的车

    横纵坐标互不影响,所以问题转化到一维:在n个区间中每个区间选一个数,n个数都被选一次 将区间按右端点排序,枚举区间,每个区间选最靠左的没被选过的点 #include<algorithm> ...

  3. 02-线性结构3 Reversing Linked List (25 分)

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  4. elasticsearch: can not run elasticsearch as root

    进入bin目录启动elasticsearch: lunadeMacBook-Air:elasticsearch-6.1.0 luna$ cd bin/ lunadeMacBook-Air:bin lu ...

  5. java中&& 的运算优先级高于||

    public class First { public static void main(String[] args) { boolean a = false; boolean b = true; b ...

  6. _blocking_errnos = {errno.EAGAIN, errno.EWOULDBLOCK} pip

    python2.6 get-pip.py 报错下面的错误_blocking_errnos = {errno.EAGAIN, errno.EWOULDBLOCK} pip解决方案:# 1, 从官方git ...

  7. 【自学Spring Boot】什么是Spring Boot

    为啥要有Spring Boot? 以前大学刚开始学java web的时候,需要搭建起web框架,当时使用的是SSH(struts+spring+hibernate),那就开始搭建吧,初学者哪里知道整套 ...

  8. 2019软工实践_Alpha(6/6)

    队名:955 组长博客:https://www.cnblogs.com/cclong/p/11913269.html 作业博客:https://edu.cnblogs.com/campus/fzu/S ...

  9. tensorflow build failed on Centos with Error: suffix or operands invalid for ""

    在redhat6.5的机器上编译tensorflow1.10,局部环境配好gcc4.8.2后,发现了如题的错误.这是关于AVX指令集识别问题.虽然gcc版本足够高,能够编出使用AVX的汇编代码,但是b ...

  10. egg.js搭建 api设置跨域

    1.egg简述 Egg.js,为企业级框架和应用而生,是阿里开源的企业级 Node.js 框架. 2.特点 Egg 奉行『约定优于配置』,按照一套统一的约定进行应用开发,团队内部采用这种方式可以减少开 ...