XML与JSON的互相转化详见:XML模块

https://www.cnblogs.com/shengyang17/p/8606223.html

<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank updated="yes">5</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank updated="yes">69</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
# coding=utf-8
import xml.etree.ElementTree as ET
tree = ET.parse("xmltest.xml") #open
root = tree.getroot() #f.seek(0) <Element 'data' at 0x027EAAE0>
print(root.tag) #data 根节点 for child in root:
print('-------------',child.tag,child.attrib) #------------- country {'name': 'Liechtenstein'}
for i in child:
print(i.tag, i.text) ##获取xml: <rank updated="yes">2</rank> <year>2008</year>
''' ##rank 2 year 2008
------------- country {'name': 'Liechtenstein'}
rank 2
year 2008
gdppc 141100
neighbor None
neighbor None
------------- country {'name': 'Singapore'}
rank 5
year 2011
gdppc 59900
neighbor None
------------- country {'name': 'Panama'}
rank 69
year 2011
gdppc 13600
neighbor None
neighbor None
''' for node in root.iter('year'): ##只遍历year节点
print(node.tag, node.text)
'''
year 2008
year 2011
year 2011
'''

event.xml

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Events>
<Event xmlns1='http://schemas.microsoft.com/win/2004/08/events/event'>
<System>
<Provider Name='Microsoft-Windows-Security-SPP' Guid='{E23B3380-C8C9-472C-F28DFEA0F156}' EventSourceName='Software Protection Platform Server'></Provider>
<EventID Qualifiers='16384'>902</EventID>
<Version>0</Version>
<Level>0</Level>
<Task>0</Task>
<Opcode>0</Opcode>
<Keywords>0x80000000000</Keywords>
<TimeCreated SystemTime='2018-12-25T09:08:59.00000000000Z'/>
<EventRecordID>8634</EventRecordID>
<Correlation/>
<Execution ProcessID='0' ThreadID='0'/>
<Channel>Application</Channel>
<Computer>WIN-CANDBPBBOCN</Computer>
<Security/>
</System>
<EventData>
<Date>6.1.7601.17514</Date>
</EventData>
<RenderingInfo Culture='zh-CN'>
<Message>软件保护服务已启动。6.1.7601.17514</Message>
<Level>信息</Level>
<Task></Task>
<Opcode></Opcode>
<Channel></Channel>
<Provider>Microsoft-Window-Security-SPP</Provider>
<keywords>
<keyword>经典</keyword>
</keywords>
</RenderingInfo>
</Event> <Event xmlns1='http://schemas.microsoft.com/win/2004/08/events/event'>
<System>
<Provider Name='Microsoft-Windows-Security-SPP' Guid='{E23B3380-C8C9-472C-F28DFEA0F156}' EventSourceName='Software Protection Platform Server'></Provider>
<EventID Qualifiers='16384'>902</EventID>
<Version>0</Version>
<Level>0</Level>
<Task>0</Task>
<Opcode>0</Opcode>
<Keywords>0x80000000000</Keywords>
<TimeCreated SystemTime='2018-12-25T09:08:59.00000000000Z'/>
<EventRecordID>8634</EventRecordID>
<Correlation/>
<Execution ProcessID='0' ThreadID='0'/>
<Channel>Application</Channel>
<Computer>WIN-CANDBPBBOCN</Computer>
<Security/>
</System>
<EventData>
<Date>6.1.7601.17514</Date>
</EventData>
<RenderingInfo Culture='zh-CN'>
<Message>软件保护服务已启动。6.1.7601.17514</Message>
<Level>信息</Level>
<Task></Task>
<Opcode></Opcode>
<Channel></Channel>
<Provider>Microsoft-Window-Security-SPP</Provider>
<keywords>
<keyword>经典</keyword>
</keywords>
</RenderingInfo>
</Event> <Event xmlns1='http://schemas.microsoft.com/win/2004/08/events/event'>
<System>
<Provider Name='Microsoft-Windows-Security-SPP' Guid='{E23B3380-C8C9-472C-F28DFEA0F156}' EventSourceName='Software Protection Platform Server'></Provider>
<EventID Qualifiers='16384'>902</EventID>
<Version>0</Version>
<Level>0</Level>
<Task>0</Task>
<Opcode>0</Opcode>
<Keywords>0x80000000000</Keywords>
<TimeCreated SystemTime='2018-12-25T09:08:59.00000000000Z'/>
<EventRecordID>8634</EventRecordID>
<Correlation/>
<Execution ProcessID='0' ThreadID='0'/>
<Channel>Application</Channel>
<Computer>WIN-CANDBPBBOCN</Computer>
<Security/>
</System>
<EventData>
<Date>6.1.7601.17514</Date>
</EventData>
<RenderingInfo Culture='zh-CN'>
<Message>软件保护服务已启动。6.1.7601.17514</Message>
<Level>信息</Level>
<Task></Task>
<Opcode></Opcode>
<Channel></Channel>
<Provider>Microsoft-Window-Security-SPP</Provider>
<keywords>
<keyword>经典</keyword>
</keywords>
</RenderingInfo>
</Event> <Event xmlns1='http://schemas.microsoft.com/win/2004/08/events/event'>
<System>
<Provider Name='Microsoft-Windows-Security-SPP' Guid='{E23B3380-C8C9-472C-F28DFEA0F156}' EventSourceName='Software Protection Platform Server'></Provider>
<EventID Qualifiers='16384'>902</EventID>
<Version>0</Version>
<Level>0</Level>
<Task>0</Task>
<Opcode>0</Opcode>
<Keywords>0x80000000000</Keywords>
<TimeCreated SystemTime='2018-12-25T09:08:59.00000000000Z'/>
<EventRecordID>8634</EventRecordID>
<Correlation/>
<Execution ProcessID='0' ThreadID='0'/>
<Channel>Application</Channel>
<Computer>WIN-CANDBPBBOCN</Computer>
<Security/>
</System>
<EventData>
<Date>6.1.7601.17514</Date>
</EventData>
<RenderingInfo Culture='zh-CN'>
<Message>软件保护服务已启动。6.1.7601.17514</Message>
<Level>信息</Level>
<Task></Task>
<Opcode></Opcode>
<Channel></Channel>
<Provider>Microsoft-Window-Security-SPP</Provider>
<keywords>
<keyword>经典</keyword>
</keywords>
</RenderingInfo>
</Event> </Events>
import xml.etree.ElementTree as ET

def es(mvalue):
pass def packNodes(nodes,m):
'''
去除空字典方法
if len(nodes) < 0 or nodes.attrib == None or nodes.text == None:
return
'''
if len(nodes) < 0:
return
tag = nodes.tag ##所有的{key: value}key值
text = nodes.text ##所有的value值
m[tag] = {} ## m = {}空字典,把key值添加到新字典中;比如:{'Events': {}} {'Event': {}}, m的value作为{}空字典
m[tag].update(nodes.attrib) #nodes.attrib是打印出 把=两边的作为{key: value }字典的形式--作为m字典的value
#print(m) #{'Events': {}} {'Event': {'xmlns1': 'http://schemas.microsoft.com/win/2004/08/events/event'}}... if text != None:
if text.strip() != "":
m[tag]["value"] = text #去除空值和空格
for node in nodes: #nodes一开始是作为根节点
packNodes(node,m[tag]) ##递归调用 def main(): # 创建文档树
tree = ET.ElementTree(file="event.xml")
# 获取根节点
root = tree.getroot()
print(root)#<Element 'Events' at 0x01EAAB40>
list = []
for r in root:
m = {}
packNodes(root,m) #
list.append(m)
# root.attrib--->字典
# root.tag --->字符串
# root.text --->字符串
# 元素封装到map中
'''
封装单个数组
m = {}
packNodes(root,m)
'''
print(list)
# es(m) if __name__ == '__main__':
main()

结果如下:

[{'Events':
{'Event': {'xmlns1': 'http://schemas.microsoft.com/win/2004/08/events/event',
'System':
{'Provider': {'Name': 'Microsoft-Windows-Security-SPP', 'Guid': '{E23B3380-C8C9-472C-F28DFEA0F156}', 'EventSourceName': 'Software ProtectionPlatform Server'},
'EventID': {'Qualifiers': '', 'value': ''},
'Version': {'value': ''}, 'Level': {'value': ''},
'Task': {'value': ''},
'Opcode': {'value': ''},
'Keywords': {'value': '0x80000000000'},
'TimeCreated': {'SystemTime': '2018-12-25T09:08:59.00000000000Z'},
'EventRecordID': {'value': ''},
'Correlation': {},
'Execution': {'ProcessID': '', 'ThreadID': ''},
'Channel': {'value': 'Application'},
'Computer': {'value': 'WIN-CANDBPBBOCN'},
'Security': {}},
'EventData': {'Date': {'value': '6.1.7601.17514'}},
'RenderingInfo': {'Culture': 'zh-CN', 'Message': {'value': '软件保护服务已启动。6.1.7601.17514'}, 'Level':{'value': '信息'}, 'Task': {}, 'Opcode': {}, 'Channel': {}, 'Provider': {'value': 'Microsoft-Window-Security-SPP'}, 'keywords': {'keyword': {'value': '经典'}}}
}}},
{'Events':
{'Event': {'xmlns1': 'http://schemas.microsoft.com/win/2004/08/events/event', 'System': {'Provider': {'Name': 'Microsoft-Windows-Security-SPP',
'Guid': '{E23B3380-C8C9-472C-F28DFEA0F156}', 'EventSourceName': 'Software Protec
tion Platform Server'}, 'EventID': {'Qualifiers': '16384', 'value': '902'}, 'Ver
sion': {'value': '0'}, 'Level': {'value': '0'}, 'Task': {'value': '0'}, 'Opcode'
: {'value': ''}, 'Keywords': {'value': '0x80000000000'}, 'TimeCreated': {'Syste
mTime': '2018-12-25T09:08:59.00000000000Z'}, 'EventRecordID': {'value': '8634'},
'Correlation': {}, 'Execution': {'ProcessID': '', 'ThreadID': ''}, 'Channel':
{'value': 'Application'}, 'Computer': {'value': 'WIN-CANDBPBBOCN'}, 'Security':
{}}, 'EventData': {'Date': {'value': '6.1.7601.17514'}}, 'RenderingInfo': {'Cul
ture': 'zh-CN', 'Message': {'value': '软件保护服务已启动。6.1.7601.17514'}, 'Lev
el': {'value': '信息'}, 'Task': {}, 'Opcode': {}, 'Channel': {}, 'Provider': {'v
alue': 'Microsoft-Window-Security-SPP'}, 'keywords': {'keyword': {'value': '经典
'}}}}}},
...]

Python解析XML文件的更多相关文章

  1. python 解析xml 文件: Element Tree 方式

    环境 python:3.4.4 准备xml文件 首先新建一个xml文件,countries.xml.内容是在python官网上看到的. <?xml version="1.0" ...

  2. python 解析xml 文件: DOM 方式

    环境 python:3.4.4 准备xml文件 首先新建一个xml文件,countries.xml.内容是在python官网上看到的. <?xml version="1.0" ...

  3. python 解析xml 文件: SAX方式

    环境 python:3.4.4 准备xml文件 首先新建一个xml文件,countries.xml.内容是在python官网上看到的. <?xml version="1.0" ...

  4. 遍历文件 创建XML对象 方法 python解析XML文件 提取坐标计存入文件

    XML文件??? xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. 里面的标签都是可以随心所欲的按照他的命名规则来定义的,文件名为roi.xm ...

  5. Python 解析 XML 文件生成 HTML

    XML文件result.xml,内容如下: <ccm> <metric> <complexity>1</complexity> <unit> ...

  6. 【TensorFlow】Python解析xml文件

    最近在项目中使用TensorFlow训练目标检测模型,在制作自己的数据集时使用了labelimg软件对图片进行标注,产生了VOC格式的数据,但标注生成的xml文件标签值难免会产生个别错误造成程序无法跑 ...

  7. Python解析xml文件遇到的编码解析的问题

    使用python对xml文件进行解析的时候,假设xml文件的头文件是utf-8格式的编码,那么解析是ok的,但假设是其它格式将会出现例如以下异常: xml.parsers.expat.ExpatErr ...

  8. [转载] python 解析xml 文件: SAX方式

    环境 python:3.4.4 准备xml文件 首先新建一个xml文件,countries.xml.内容是在python官网上看到的. <?xml version="1.0" ...

  9. python解析xml文件时使用ElementTree和cElementTree的不同点;iter

    在python中,解析xml文件时,会选用ElementTree或者cElementTree,那么两者有什么不同呢? 1.cElementTree速度上要比ElementTree快,比较cElemen ...

  10. python 解析 XML文件

    如下使用xml.etree.ElementTree模块来解析XML文件.ElementTree模块中提供了两个类用来完成这个目的: ElementTree表示整个XML文件(一个树形结构) Eleme ...

随机推荐

  1. 多输入select

    目录 多输入select IO模型 select介绍 小demo 注意 引入电子书 title: 多输入select date: 2019/3/20 17:21:34 toc: true --- 多输 ...

  2. C语言 内存管理(转)

     转自 https://blog.csdn.net/u011616739/article/details/61621815 C语言 内存管理 1.内存分区 C源代码进过预处理.编译.汇编和链接4步生成 ...

  3. java 多线程 synchronized与lock的通信机制等问题,结合相应实例说明

    1. 利用多线程实现如下需求: 写两个线程,一个线程打印1~52,另一个线程打印A~Z,打印顺序是12A34B...5152Z: 2. 使用synchronized 实现 public class T ...

  4. ORM系列之Entity FrameWork详解

    一. 谈情怀 从第一次接触开发到现在(2018年),大约有六年时间了,最初阶段连接数据库,使用的是[SQL语句+ADO.NET],那时候,什么存储过程.什么事务 统统不理解,生硬的将SQL语句传入SQ ...

  5. C#代码使用Process类调用SWFTools工具

    一.Process类调用SWFTools工具将PDF文档转为swf文档 1 string cmdStr = "D:\\SWFTools\\pdf2swf.exe"; string ...

  6. Qt快速入门第三版下载

    找了半天才找到一个靠谱的: http://www.ddooo.com/softdown/111665.htm

  7. THUWC2019 GG记

    所以要什么时候才不会出现像这样的滚粗记呢? Day-?~Day-4 我也不清楚具体干了什么 不过学了很多东西,至少相对于去年还是个小菜鸡,今年已经变成大菜鸡了 Day-3~Day-1 几乎就是复习前面 ...

  8. I\O操作

    作用:读写设备上数据.硬盘文件.内存.键盘.网络等. 分类: 数据走向:输入流.输出流 数据类型:字符流(文本数据Reader或者Writer结尾) 字节流(所有类型Stream结尾) 1个字节 = ...

  9. DC综合简单总结(1)

     DC综合简单总结(1)  *****************set_dont_touch和set_dont_touch_network**************** ? 在综合的过程中,为了不让D ...

  10. 论文笔记:Fast(er) RCNN

    在 RCNN 初步试水取得成功后,研究人员又迅速跟进,针对 RCNN 中的几点不足提出改进,接连推出了 fast-rcnn 和 faster-rcnn.关于这两篇论文,网上相关的文章实在是多如牛毛,因 ...