xml数据示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year updated_by="Alex">2009</year>
        <gdppc>141100</gdppc>
        <neighbor direction="E" name="Austria" />
        <neighbor direction="W" name="Switzerland" />
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year updated_by="Alex">2012</year>
        <gdppc>59900</gdppc>
        <neighbor direction="N" name="Malaysia" />
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year updated_by="Alex">2012</year>
        <gdppc>13600</gdppc>
        <neighbor direction="W" name="Costa Rica" />
        <neighbor direction="E" name="Colombia" />
        <info>
            <population>8</population>
            <size>960</size>
        </info>
    </country>
</data>

xml数据处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import xml.etree.ElementTree as ET
 
'''xml 数据的处理 '''
tree = ET.parse("xmltest.xml")
root = tree.getroot() #数据内存地址
print(root.tag)  #标签
 
'''遍历所有数据'''
for i in root:
    print(i.tag,i.attrib)  #attrib 获取属性名
    for k in i:
        print(k.tag,k.attrib,k.text) #text 文本内容
 
''' 遍历某一个标签的值 '''
for ta in root.iter("year"):
    print(ta.tag,ta.attrib,ta.text)

XML数据的创建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
'''xml 数据的创建 '''
new_xml = ET.Element("personinfolist") #Element 根节点
personinfo = ET.SubElement(new_xml, "personinfo", attrib={"enrolled": "yes"})
name = ET.SubElement(personinfo, "name"#SubElement 子节点
name.text = "Alex Li"
age = ET.SubElement(personinfo, "age", attrib={"checked": "no"})
sex = ET.SubElement(personinfo, "sex")
age.text = '56'
personinfo2 = ET.SubElement(new_xml, "personinfo", attrib={"enrolled": "no"})
name = ET.SubElement(personinfo2, "name")
name.text = "Oldboy Ran"
age = ET.SubElement(personinfo2, "age")
age.text = '19'
 
et = ET.ElementTree(new_xml)  # 生成文档对象
et.write("test.xml", encoding="utf-8", xml_declaration=True)
""" xml_declaration 声明xml文件类型 """
ET.dump(new_xml)  # 打印生成的格式

XML数据的修改

1
2
3
4
5
6
7
8
9
10
11
12
13
'''xml 数据的修改 '''
for node in root.iter('year'):
    new_year = int(node.text) + 1
    node.text = str(new_year)
    node.set("updated_by", "Alex")
tree.write("xmltest.xml")
 
# 删除node
for country in root.findall('country'):
    rank = int(country.find('rank').text)
    if rank > 50:
        root.remove(country)
tree.write('output.xml')

Python3 xml模块的增删改查的更多相关文章

  1. java对xml文件做增删改查------摘录

    java对xml文件做增删改查 package com.wss; import java.io.File;import java.util.ArrayList;import java.util.Lis ...

  2. 基于pymysql模块的增删改查

    上课笔记 重点:(熟练)多表查询创建存储过程原生sql索引原理 pymysql 封装好的客户端cursor 底层就是一个send操作commit 告诉mysql真的要完成修改操作(不然修改不会生效)e ...

  3. 使用dom4j对xml文件进行增删改查

    1.使用dom4j技术对dom_demo.xml进行增删改查 首选要下载dom4j的jar包 在官网上找不到,网上搜索了一下在这个链接:http://sourceforge.net/projects/ ...

  4. XML(五)dom4j增删改查

    book2.xml <? xml version="1.0" encoding="UTF-8"?> <书架> <书> < ...

  5. python3连接MySQL实现增删改查

    PyMySQL 安装 在使用 PyMySQL 之前,我们需要确保 PyMySQL 已安装. PyMySQL 下载地址:https://github.com/PyMySQL/PyMySQL. 如果还未安 ...

  6. 用pickle模块实现“增删改查”的简易功能

    pickle的作用: 1:pickle.dump(dict,file)把字典转为二进制存入文件. 2:pickle.load(file)把文件二进制内容转为字典 import pickle # 增 d ...

  7. Asp.Net 操作XML文件的增删改查 利用GridView

    不废话,直接上如何利用Asp.NET操作XML文件,并对其属性进行修改,刚开始的时候,是打算使用JS来控制生成XML文件的,但是最后却是无法创建文件,读取文件则没有使用了 index.aspx 文件 ...

  8. java图书管理的一个小模块(增删改查,不使用数据库)

    图书管理模块:某图书管需要对图书进行信息化管理,要求管理员能够进行新增图书,能按照书名进行模糊查看图书能进行价格统计 系统实现如下:1.新增2.查询3.统计价格 1请输入新书:图书号,书名,作者,价格 ...

  9. dom4j解析xml文档(增删改查)

    package itcast.dom4j; import java.io.File; import java.io.FileOutputStream; import java.io.FileWrite ...

随机推荐

  1. css3 字体渐变

    先看个效果 https://www.bienvillecapital.com/ 然后人家样式这样写的 font-family: Overpass,Helvetica,sans-serif; font- ...

  2. 【bzoj1737】[Usaco2005 jan]Naptime 午睡时间 dp

    题目描述 Goneril is a very sleep-deprived cow. Her day is partitioned into N (3 <= N <= 3,830) equ ...

  3. JavaScript中Switch使用

    switch 语句用于基于不同的条件来执行不同的动作.使用 switch 语句来选择要执行的多个代码块之一. switch(n) { case 1: 执行代码块 1 break; case 2: 执行 ...

  4. 【题解】CF#855 G-Harry Vs Voldemort

    个人感觉挺有意思的,然而被颜神D无聊惹(- ̄▽ ̄)- 这题我们可以首先试图去统计以每一个点作为 w 点所能对答案造成的贡献是多少.不难发现,当且仅当 u 和 v 都在 w 所在边双的一侧的时候不能构成 ...

  5. [洛谷P3627][APIO2009]抢掠计划

    题目大意:给你一张$n(n\leqslant5\times10^5)$个点$m(m\leqslant5\times10^5)$条边的有向图,有点权,给你起点和一些可能的终点.问从起点开始,到任意一个终 ...

  6. BZOJ1566:[NOI2009]管道取珠——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=1566 https://www.luogu.org/problemnew/show/P1758 题目 ...

  7. BZOJ5301:[CQOI2018]异或序列——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=5301 https://www.luogu.org/problemnew/show/P4462 已知 ...

  8. HDOJ(HDU).1241 Oil Deposits(DFS)

    HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  9. 【优先队列】【UVa11997】K Smallest Sums

    传送门 Description Input Output Translation · 给定k个长度为k的数组,把每个数组选一个元素加起来,这样共有kk种可能的答案,求最小的k个 Sample Inpu ...

  10. vue中使用 contenteditable 制作输入框并使用v-model做双向绑定

    <template> <div class="div-input" :class="value.length > 0 ? 'placeholder ...