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. 关于std::cin阻塞事件循环以及控制台命令输入功能的方案;

    如果将std::cin建立在主线程,将阻塞主线程的所有事件循环,因而要保证事件循环不受影响,需要将std::cin放到 子线程中.同时,如果要实现能够强制终止std::cin阻塞, 可以通过强制终止线 ...

  2. 细说java系列之HashMap原理

    目录 类图 源码解读 总结 类图 在正式分析HashMap实现原理之前,先来看看其类图. 源码解读 下面集合HashMap的put(K key, V value)方法探究其实现原理. // 在Hash ...

  3. bzoj 4244 括号序列dp

    将各种情况绕环等看作括号序列,括号内的区域上下都需要累加答案,左右也是 f[i][j] 代表 前i个车站已经处理完的有j个左括号的最小权值 我们可以发现,更新的来源来自于 i-1, 和 i 将上 描述 ...

  4. flex弹性盒子的使用

    前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! CSS3引入了一种新的布局模型—— flex 布局.flex是 flexible box 的缩写,一般称之 ...

  5. Java Socket 服务端发送数据 客户端接收数据

    服务端: package com.thinkgem.wlw.modules.api.test.socket; /** * @Author: zhouhe * @Date: 2019/4/8 9:30 ...

  6. idea integrate project

    idea的integrate project功能,版本控制工具:svn 之前我对这个功能的误解太深了,这里特别记录一下这个功能的使用,首先上图 先看这里的source1和source2,里面填的是sv ...

  7. 阿里云服务器+ftp文件操作+基于Centos7的vsftpd配置

    路径问题:一定要注意此位置是否需要加入"/" 文件上传方式:被动模式 vsftp完整配置: # # The default compiled in settings are fai ...

  8. python3字符串

    Python3 字符串 Python字符串运算符 + 字符串连接 a + b 输出结果: HelloPython * 重复输出字符串 a*2 输出结果:HelloHello [] 通过索引获取字符串中 ...

  9. python爬虫之xpath的基本使用

    一.简介 Xpath是一门在XML文档中查找信息的语言.Xpath可用来在XML文档中对元素和属性进行遍历.Xpath是W3C XSLT标准的主要元素,并且XQuery和XPointer都构建于XPa ...

  10. nginx 配置proxy_pass URL末尾加与不加/(斜线)的区别

    nginx在配置proxy_pass的时候 URL结尾加斜线(/)与不加的区别和注意事项 假设访问路径的 /pss/bill.html 加/斜线的情况 location /pss/ { proxy_p ...