zoukankan      html  css  js  c++  java
  • xml.etree.ElementTree模块

      xml.etree.ElementTree模块

    Element类型是一种灵活的容器对象,用于在内存中存储结构化数据。

      xml.etree.ElementTree模块在应对恶意结构数据时显得并不安全。

    每个element对象都具有以下属性:

      1. tag:string对象,表示数据代表的种类。

      2. attrib:dictionary对象,表示附有的属性。

      3. text:string对象,表示element的内容。

      4. tail:string对象,表示element闭合之后的尾迹。

      5. 若干子元素(child elements)。

    XML文件内容:

    复制代码
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <zabbix_export>
     3     <version>3.2</version>
     4     <date>2017-10-27T08:30:54Z</date>
     5     <groups>
     6         <group>
     7             <name>JX-SERVER</name>
     8         </group>
     9     </groups>
    10     <hosts>
    11         <host>
    12             <host>GYSERVER</host>
    13             <name>GYSERVER</name>
    14             <description/>
    15             <proxy/>
    16             <status>0</status>
    17             <ipmi_authtype>-1</ipmi_authtype>
    18             <ipmi_privilege>2</ipmi_privilege>
    19             <ipmi_username/>
    20             <ipmi_password/>
    21             <tls_connect>1</tls_connect>
    22             <tls_accept>1</tls_accept>
    23             <tls_issuer/>
    24             <tls_subject/>
    25             <tls_psk_identity/>
    26             <tls_psk/>
    27             <templates>
    28                 <template>
    29                     <name>Template OS Windows</name>
    30                 </template>
    31             </templates>
    32             <groups>
    33                 <group>
    34                     <name>JX-SERVER</name>
    35                 </group>
    36             </groups>
    37             <interfaces>
    38                 <interface>
    39                     <default>1</default>
    40                     <type>1</type>
    41                     <useip>1</useip>
    42                     <ip>172.17.200.116</ip>
    43                     <dns/>
    44                     <port>10050</port>
    45                     <bulk>1</bulk>
    46                     <interface_ref>if1</interface_ref>
    47                 </interface>
    48             </interfaces>
    49             <applications/>
    50             <items/>
    51             <discovery_rules/>
    52             <httptests/>
    53             <macros/>
    54             <inventory/>
    55         </host>
    56     </hosts>
    57 </zabbix_export>
    复制代码

    一、使用xml.etree.ElementTree模块读取XML

    复制代码
    1 import xml.etree.ElementTree as ET
    2 tree = ET.parse('test.xml')
    3 root = tree.getroot()
    4 print(root.tag)#根节点标签
    5 for child in root:
    6      print(child.tag,child.attrib,child.text)  #二级节点标签、属性、内容
    复制代码

    输出结果:

    复制代码
    1 zabbix_export #
    2 version {} 3.2
    3 date {} 2017-10-27T08:30:54Z
    4 groups {} 
    5         
    6 hosts {} 
    复制代码

     这是使用for函数查询根节点下所有元素的方式,另外一种是使用iter('text')查询特定的节点下所有的元素,来看下面这段代码,查询某个特定节点。

    复制代码
    1 import xml.etree.ElementTree as ET
    2 tree = ET.parse('test.xml')
    3 root = tree.getroot()
    4 for node in root.iter('host'): #查询host节点
    5      for i in node: #查询host节点子节点
    6           print(i.tag,i.text)
    复制代码

    输出结果:

    复制代码
     1 host GYSERVER
     2 name GYSERVER
     3 description None
     4 proxy None
     5 status 0
     6 ipmi_authtype -1
     7 ipmi_privilege 2
     8 ipmi_username None
     9 ipmi_password None
    10 tls_connect 1
    11 tls_accept 1
    12 tls_issuer None
    13 tls_subject None
    14 tls_psk_identity None
    15 tls_psk None
    16 templates 
    17 # template下还有子节点,所以此行为空,以下同理                
    18 groups 
    19                 
    20 interfaces 
    21                 
    22 applications None
    23 items None
    24 discovery_rules None
    25 httptests None
    26 macros None
    27 inventory None
    复制代码

    上面这种查询方式查找的是整个XML内符合所有条件的值。

    二、使用xml.etree.ElementTree模块修改XML

    xml.etree.ElementTree模块修改无法直接修改原值,通过采用重新赋值的方式修改。

    复制代码
    1 import xml.etree.ElementTree as ET
    2 tree = ET.parse('test.xml')
    3 root = tree.getroot()
    4 for node in root.findall('hosts'):
    5      print(node.tag,node.text)
    6      temp_node = 'hostsssss'
    7      node.tag = temp_node
    8      print(node.tag,node.text)
    9 tree.write('test.xml') #保存
    复制代码

    输出结果:

    1 hosts 
    2         
    3 hostsssss 
    4         

    三、使用xml.etree.ElementTree模块创建XML

    复制代码
    1 from xml.etree import ElementTree as ET
    2 
    3 root = ET.Element('root',{'age':'18'})#设置属性age=18
    4 
    5 son=ET.SubElement(root,'root',{'age':'18'})
    6 ET.SubElement(son,'haha',{'bb':'fhfhf'})
    7 
    8 tree = ET.ElementTree(root)
    9 tree.write('aha.xml') #保存aha.xml文件
    复制代码

    输出结果:

    1 <root age="18"><root age="18"><haha bb="fhfhf" /></root></root>
  • 相关阅读:
    Backtrader中文笔记之Renko Bricks
    Renko Charts介绍
    Backtrader中文笔记之Cerebro(大脑)。
    Backtrader中文笔记之Operating the platform(操作平台)。
    Backtrader中文笔记之Platform Concepts(平台介绍)。
    Backtrader中文笔记_Quickstart。
    PyAlgoTrade 0.20中文笔记
    浅谈JS中 reduce() 的用法(转帖)
    Python websocket的示例(转帖)
    硬盘显示有容量,但无法放入文件,还有一个查看机器端口是否开放。
  • 原文地址:https://www.cnblogs.com/zhouzhou0/p/10525919.html
Copyright © 2011-2022 走看看