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>
View Code

相关文章: