python—xml读写—minidom and etree
来源:互联网 发布:mysql从零开始学 编辑:程序博客网 时间:2024/06/05 21:00
test.xml
<?xml version="1.0" encoding="UTF-8"?> <employees> <employee id="0"> <name>Liu</name> <age>30</age> </employee> <employee id="1"> <name>John</name> <age>20</age> </employee> </employees>
from xml.dom import minidom from codecs import openif __name__ == '__main__': #用xml.dom.minidom读xml doc=minidom.parse('test.xml') root=doc.documentElement #得到根节点——employees employees=root.getElementsByTagName('employee') #根据名字来查找子elements for employee in employees: print ("id:"+employee.getAttribute('id')) #获得属性值 name=employee.getElementsByTagName('name')[0] #childNodes返回所有的子Nodes,其中所有的文本均为textNode,包括元素间的‘\n\r’和空格均 print(name.nodeName+":"+name.childNodes[0].nodeValue) #nodeValue是结点的值,只对textNode有效 age=employee.getElementsByTagName('age')[0] print(age.nodeName+":"+age.childNodes[0].data) #对于textNode,得到文本内容也可以使用.data #用xml.dom.minidom写xml impl=minidom.getDOMImplementation() doc=impl.createDocument(None, 'employees', None) #创建根节点——employees root=doc.documentElement employee=doc.createElement('employee') #创建employee节点 employee.setAttribute('id', '0') #设置id属性 root.appendChild(employee) namenode=doc.createElement('name') #创建name节点 nametext=doc.createTextNode('刘') #创建textnode namenode.appendChild(nametext) employee.appendChild(namenode) agenode=doc.createElement('age') agetext=doc.createTextNode('30') agenode.appendChild(agetext) employee.appendChild(agenode) f=open('write.xml','w',encoding='utf-8') #addindent表示子元素缩进,newl='\n'表示元素间换行,encoding='utf-8'表示生成的xml的编码格式(<?xml version="1.0" encoding="utf-8"?>) doc.writexml(f, addindent=' ', newl='\n', encoding='utf-8')
输出结果:
id:0name:Liuage:30id:1name:Johnage:20
write.xml:
<?xml version="1.0" encoding="utf-8"?><employees> <employee id="0"> <name>刘</name> <age>30</age> </employee></employees>
2. etree
from xml.etree import ElementTree#打印节点信息def print_node(node): print "-------------------------------" print "attrib:%s" %node.attrib #属性的字典 print "id:%s" %node.attrib['id'] #指定的属性值 for key,value in node.items(): #items:属性(name, value)对儿 print "%s:%s" %(key,value) for child in node: print "tag:%s" %child.tag print "text:%s" %child.text#读xmldef readxml(text='',xmlfile=''): #root=ElementTree.parse('test.xml').getroot() #加载指定文件获得根节点 root=ElementTree.fromstring(text) #加载指定字符串获得根节点 employees=root.iter('employee') #通过iter(tag=None)获取element for employee in employees: print_node(employee) for employee in root.findall('employee'): #findall:匹配的直接子节点 print "-------------------------------" age=employee.find('age').text #find:第一个匹配的 id=employee.get('id') #得到属性值 print age,id #更改xmldef updatexml(text='',xmlfile=''): tree=ElementTree.parse(xmlfile) #加载指定文件 root=tree.getroot() #获得根节点 for age in root.iter('age'): #获得所有age节点 newage=int(age.text)+1 age.text=str(newage) age.set('updated','yes') #设置属性-值 name=root[0].find('name') root[0].remove(name) #移除节点 #创建节点 employee=ElementTree.Element('employee',{'id':'99'}) #Element(tag, attrib={}, **extra) name=ElementTree.SubElement(employee, 'name') #SubElement(parent, tag, attrib={}, **extra) name.text='新' age=ElementTree.SubElement(employee,'age') age.text='99' root.append(employee) #write(file, encoding="us-ascii", xml_declaration=None, default_namespace=None, method="xml") tree.write('updated.xml',encoding='utf-8') if __name__ == '__main__': readxml(open('test.xml').read()) updatexml('','test.xml')
输出结果:
-------------------------------attrib:{'id': '0'}id:0id:0tag:nametext:Liutag:agetext:30-------------------------------attrib:{'id': '1'}id:1id:1tag:nametext:Johntag:agetext:20-------------------------------30 0-------------------------------20 1
<employees> <employee id="0"> <age updated="yes">31</age> </employee> <employee id="1"> <name>John</name> <age updated="yes">21</age> </employee> <employee id="99"><name>新</name><age>99</age></employee></employees>
用SubElement好像没法换行?
0 0
- python—xml读写—minidom and etree
- Python 使用minidom读写xml
- python类库31[使用minidom读写xml]
- python类库31[使用minidom读写xml]
- 使用minidom读写xml
- python类库31[使用xml.etree.ElementTree读写xml]
- python类库[使用xml.etree.ElementTree读写xml]
- python minidom读xml
- python minidom 写xml
- python minidom读xml
- python minidom 处理XML
- Python--xml.dom.minidom
- python - xml.etree
- python minidom 写xml示例 .
- python-minidom模块【解析xml】
- python-minidom模块【解析xml】
- Python解析XML的minidom
- python解析xml文件——通过etree来解析xml文件
- 计算器之逆波兰算法
- Zend API: 支持初始化文件(php.ini)
- Qt的Linux 线程中传递,类的类型参数
- [LeetCode] Letter Combinations of a Phone Number
- 【Ubuntu】[VIM]:ubuntu下vi不能正常使用的解决方法
- python—xml读写—minidom and etree
- Opengl es2.0 glEnable(GL_TEXTURE_2D)报错 GL_INVALID_ENUM
- Sharepoint2013 Report Service初探
- 与android database 交互(chapter 14-Working with the SQLite Database)
- Spark安装启动 and 在程序中调用spark服务
- linux 查看 进程 端口号
- 树状数组解敌兵布阵
- Hadoop2.2.0的eclipse插件的编译
- Hashtable源码分析