• xml操作


    xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融行业的很多系统的接口还主要是xml。

    xml的格式如下,就是通过<>节点来区别数据结构的:

    <?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>

    python 操作xml文件:

     1 import xml.etree.ElementTree as ET
     2  
     3 tree = ET.parse("xmltest.xml")
     4 root = tree.getroot()#找到根目录
     5 print(root.tag)
     6  
     7 #遍历xml文档
     8 for child in root:
     9     print(child.tag, child.attrib)
    10     for i in child:
    11         print(i.tag,i.text)#输出节点信息
    12  
    13 #只遍历year 节点
    14 for node in root.iter('year'):
    15     print(node.tag,node.text)
    16 小结:tag--标签
    17          attrib--标签属性
    18          text--文本内容
    View Code

    修改删除创建参考http://www.cnblogs.com/alex3714/articles/5161349.html

    是什么让你疯狂?
  • 相关阅读:
    存货核算中的加权平均
    System stored procedures
    javascript中replace与正则表达式
    AfxGetApp 解剖
    MFC中的文字输出
    函数指针的应用
    MFC消息处理与SDK消息处理
    在本地机器上获取root权限
    文件内容读取
    Linux下安装windows系统的方法
  • 原文地址:https://www.cnblogs.com/iforelse/p/6877935.html
Copyright © 2020-2023  润新知