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>
import xml.etree.ElementTree as ET
tree = ET.parse('xz.xml')     #打开文件
root = tree.getroot()
print(root.tag)

#遍历文档
for child in root:
    print(child.tag,child.attrib)
    for i in child:
        print(i.tag,i.text)

#遍历year节点
for node in root.iter('year'):
    print(node.tag,node.text)
---------------------------------------------------------------------------
import xml.etree.ElementTree as ET
tree = ET.parse('xz.xml')     #打开文件
root = tree.getroot()             #seek(0)
#修改
for node in root.iter('year'):
    new_year = int(node.text) + 10
    node.text = str(new_year)
    node.set('newcoding','yes')
tree.write('test.xml')    
#删除
for country in root.findall('country'):
    rank = int(country.find('rank').text)
    if rank > 50:
        root.remove(country)

tree.write('outtest.xml')

自建xml

import xml.etree.ElementTree as ET

root = ET.Element('nameroot')
people = ET.SubElement(root,'people',attrib = {'enrolled':'yes'})
name = ET.SubElement(people,'name')
name.text = 'Bob'
age = ET.SubElement(people,'age',attrib={'enrooled':'no'})
gender = ET.SubElement(people,'gender')
gender.text = 'male'
people = ET.SubElement(root,'people',attrib = {'enrolled':'yes'})
name = ET.SubElement(people,'name')
name.text = 'Lucy'
age = ET.SubElement(people,'age')
age = '18'
gender = ET.SubElement(people,'gender')
gender.text = 'female'

et = ET.ElementTree(root)
et.write('xmltest.xml',encoding='utf8',xml_declaration=True)

ET.dump(root)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
zyXML 是一个轻便、小巧的,纯易语言代码来解析XML模块。 优点: 1、不依赖任何DLL、支持库、系统组件(Microsoft.XMLDOM等对象) 2、支持UTF-8 、UTF-16 等编码 3、支持 XPath 语法来查询节点 4、支持 CDATA 5、开源(如果有任何问题可以自己修改) 缺点: 1、加载速度稍微逊色于支持库,但是查询速度很快。 2、不支持DTD,也就是类似于如下的节点 <!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)> <!ELEMENT to      (#PCDATA)> <!ELEMENT from    (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body    (#PCDATA)> ]> 3、XPath查询语法支持不全(要全部支持太麻烦了,实在没精力去解析了) XPath具体语法可参考:http://www.w3school.com.cn/xpath/xpath_syntax.asp 目前支持的XPath 语法有限,不支持的有: 1、查找只能返回节点,而不能返回属性,当然查找条件可以有属性,如:节点名[@属性\u540d='\u5c5e\u6027\u503c'] 2、[] 括号里面的谓语表达式 仅仅支持一个操作符号,且不支持 | 、 or 、 and 和 mod ,并且属性名不支持*,仅仅元素名才支持* 3、轴 不支持 following、namespace、preceding 和 preceding-sibling 4、函数都不支持,但是可以有如下使用: node()         可返回所有节点且不管节点是什么类型 cdata()        仅查找 CDATA 类型的节点 text()           仅查找 文本 节点 comment()  仅查找 注释 节点 *                  仅查找 元素 节点
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值