zoukankan      html  css  js  c++  java
  • 14.python的xml操作

    #-*-coding:UTF-8-*-
    #python xml 文件操作
    import xml.etree.ElementTree as etree       #ElementTree属于python标准库的一部分
    tree=etree.parse('feed.xml')    #parse()函数会立即解析完整个文档,返回一个代表整片文档的对象
    root=tree.getroot()             #得到根元素
    print root.tag                 #得到跟元素标签
    print len(root)                #得到根元素下一共有几个子元素
    
    for child in root:
        print child
    #获取属性
    print root.attrib              #得到元素的属性集合,为一个字典
    
    print root[0]                  #取其第一个子元素
    
    #查找结点
    print root.findall('c')         #找到则返回这个结点,没有则为一个空列表,只会搜索子元素
    
    print root[0].find('b1')        #返回找到的第一个元素
    
    #生成XML
    new_feed=etree.Element('e')
    print new_feed

  • 相关阅读:
    去重
    JavaScript 数组
    变量
    2017.3.19 for
    2017.3.19 if,for
    2017.3.14
    2017.3.10
    2017.3.9
    2017.3.7-1
    2017.3.7
  • 原文地址:https://www.cnblogs.com/chenjianhong/p/4145129.html
Copyright © 2011-2022 走看看