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': '经典
'}}}}}},
...]

最新文章

  1. 使用do{ } while(0)的好处
  2. B2B商城网站前端开发
  3. nosql数据库学习
  4. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q115-Q117)
  5. JavaScript 内部对象
  6. 【原】1.1RDD源码解读(二)
  7. .net中XML的创建02(linqToXml)
  8. poj 3301 Texas Trip(几何+三分)
  9. asp.net 网站所有请求跳转到同一个页面
  10. nginx安装与应用
  11. Python + PyQt5 实现美剧爬虫可视工具(二)
  12. 选择排序(JAVA实现)
  13. 如何开发一款html5(H5)跨平台 k12动画/交互课件/游戏
  14. 2018.11.17 codechef PRIMEDST(点分治+fft)
  15. POJ 2498
  16. MariaDB&#160;Centos7&#160;下安装MariaDB
  17. BAT-局域网内在线电脑IP
  18. 09 ORM 多表操作,创建表,添加记录
  19. WCF服务端调用client.
  20. 【Java】包装类总结

热门文章

  1. STL仿函数functor
  2. CSS 分栏结构
  3. 1. Nagios和 NagiosQL安装及配置
  4. 【转载】VS写汇编程序01:VS2015配置汇编语言开发环境
  5. JS 获取图片的base64编码
  6. linxu上安装mongodb3.6实战
  7. Linux下批量杀掉 包含某个关键字的 程序进程
  8. 如何使用门罗币远程节点remote node?
  9. FATAL ERROR: please install the following Perl modules before executing
  10. windows 2012 如何设置取消禁拼ping