• xml模块/三层结构


    day19

    一、xml模块

    1、xml的定义:

      xml是一种可扩展的标记语言,用标签来组织数据的语言。

    2、xml的特点

      相比于json,xml的使用场景更加广泛,但是语法格式相比json复杂很多。

      json:前后台交互数据时,一般使用json

      xml:当需要自定义文档时使用xml

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

    3、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>
    4、xml中获取标签的三种方法:
    # 第一种获取标签的方式
    # root.iter() 全文查找
    # 获取迭代器 如果不指定参数 则迭代器迭代的是所有标签
    # print(root.iter())
    # 获取内容是一个迭代器 如果指定参数 则迭代器迭代的是所有名称匹配的标签
    # for e in root.iter("rank"):
    #     print(e)
    # 第二种获取标签的方式
    # 在当前标签下(所有子级标签)寻找第一个名称匹配的标签
    # print(root.find("rank"))
    # 第三种获取标签的方式
    # 在当前标签下(所有子级标签)寻找所有名称匹配的标签
    # print(root.findall("rank"))
    5、xml中每个标签中所包含的三个内容:
    #标签的三个内容
    #print(element.tag)    # 标签名称
    #print(element.attrib) # 属性字典类型
    #print(element.text)   # 文本内容
    6、xml内容修改操作:
    import xml.etree.ElementTree as ET
    tree = ET.parse("xmltest.xml")
    root = tree.getroot()
     
    #修改
    for node in root.iter('year'):
        new_year=int(node.text)+1  #xml中的信息为字符串形式  
        node.text=str(new_year)
        node.set('updated','yes')  #设置属性
        node.set('version','1.0')  #设置属性
    tree.write('test.xml') #修改后要将修改后的内容写入文件
      
    #删除node
    for country in root.findall('country'):
       rank = int(country.find('rank').text)
       if rank > 50:
         root.remove(country)   #xlm中的删除方法remove
    tree.write('output.xml')
    ​
    #在country内添加(append)节点year2
    import xml.etree.ElementTree as ET
    tree = ET.parse("a.xml")
    root=tree.getroot()   
    for country in root.findall('country'):
        for year in country.findall('year'):
            if int(year.text) > 2000:
                year2=ET.Element('year2') #标签组成部分标签名
                year2.text='新年'          #标签组成部分文本
                year2.attrib={'update':'yes'} #标签组成部分属性
                country.append(year2) #往country节点下添加子节点
    ​
    tree.write('a.xml.swap')
    # 注:().append只能给()这一级添加子节点,不能给其他级添加。
    7、用python语言编写xml文档:
    import xml.etree.ElementTree as ET
      
    new_xml = ET.Element("namelist")  #获取xml对象 根标签
    name = ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"}) #父级,标签名,属性
    age = ET.SubElement(name,"age",attrib={"checked":"no"})
    sex = ET.SubElement(name,"sex")
    sex.text = '33' #文本
    name2 = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"}) #父级,标签名,属性
    age = ET.SubElement(name2,"age")
    age.text = '19'
     
    et = ET.ElementTree(new_xml)  #生成文档对象
    et.write("test.xml", encoding="utf-8",xml_declaration=True)
            # 文件名     字符编码    版本声明
    ET.dump(new_xml)  #打印生成的格式

    二、三层结构

    1、三层指的是哪三层:

      a、界面层

      b、业务逻辑层

      c、数据访问层

    2、他们的作用:

      a、界面层:主要对用户的请求接受,以及数据的返回,为客户端提供应用程序的访问。

      b、业务逻辑层:主要负责对数据层的操作。也就是说把一些数据层的操作进行组合。

      c、数据访问层:主要看数据层里面有没有包含逻辑处理,实际上它的各个函数主要完成各个对数据文件的操作。而不必管其他操作。

  • 相关阅读:
    R语言编程艺术(2)R中的数据结构
    R语言编程艺术(1)快速入门
    R语言实战(十)处理缺失数据的高级方法
    R语言实战(九)主成分和因子分析
    R语言实战(八)广义线性模型
    R语言实战(七)图形进阶
    R语言实战(六)重抽样与自助法
    R语言实战(五)方差分析与功效分析
    R语言实战(四)回归
    R语言实战(三)基本图形与基本统计分析
  • 原文地址:https://www.cnblogs.com/peng-zhao/p/10104373.html
Copyright © 2020-2023  润新知