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. C# activex开发中 axwebbrowser控件及 IE浏览器设置

    <object type='application/x-vlc-plugin' id='vlc' events='True' codebase='../cab/axvlc.cab' classi ...

  2. 三、Dockerfile的说明和编写

    一.docker hub的使用和说明: 我们要去找镜像,一般都会 https://hub.docker.com  的网站中去找,目前linux的容器,我们目前首选是alpine,因为容器非常小,可以满 ...

  3. Pushgateway 介绍

    Pushgateway是一个独立的服务,Pushgateway位于应用程序发送指标和Prometheus服务器之间. Pushgateway接收指标,然后将其作为目标被Prometheus服务器拉取. ...

  4. ModBus-RTU详解

      Modbus 一个工业上常用的通讯协议.一种通讯约定.Modbus协议包括RTU.ASCII.TCP.其中MODBUS-RTU最常用,比较简单,在单片机上很容易实现.虽然RTU比较简单,但是看协议 ...

  5. Hadoop集群管理

    1.简介 Hadoop是大数据通用处理平台,提供了分布式文件存储以及分布式离线并行计算,由于Hadoop的高拓展性,在使用Hadoop时通常以集群的方式运行,集群中的节点可达上千个,能够处理PB级的数 ...

  6. (七)File 文件的操作

    一.文件读写模式 1.文件的几种模式: 格式:f=open("文件名","模式",encode="utf-8") #文件的只读模式 f1=o ...

  7. 好的java资源地址

    前人栽树,后人乘凉.想当初自己初学Java时为了解决一个很基础的问题,好多的朋友热心的回复我,帮我分析错误.现在为了方便那些Java新手,特给出自己感觉比较好的学习网站和论坛,希望对朋友们能有点帮助. ...

  8. 小程序通过 url 向内嵌 H5 传参注意事项

    当在小程序中通过 url 向 <web-view> 内嵌的 H5 传参时,当包含特殊字符时需要进行编码处理(不然 <web-view> 中是拿不到值的,小程序竟然没有错误提示. ...

  9. Matcher.replaceFirst(String replacement)

    java.util.regex.Matcher.replaceFirst(String replacement)方法是用来进行字符串的替换操作. public String replaceFirst( ...

  10. Python的re模块中search与match的区别

    1.search和match: search:在整个字符中匹配,如果找不到匹配的就返回None match:在字符串开始位置匹配如果不匹配就返回None 2.效率对比: search: match: