zoukankan      html  css  js  c++  java
  • 创建xml文件、解析xml文件

     
     

     1.创建XML文件:

    import codecs import xml.dom.minidom

    doc=xml.dom.minidom.Document() print doc

    root=doc.createElement("booklist")

    print u"添加的xml标签为",root.tagName

    root.setAttribute("type","science and engineering")

    value=root.getAttribute("type")

    print u"root元素的'type'属性值为:",value

    #添加第一本书:

    book=doc.createElement("book")

    book.setAttribute("category","math")

    title=doc.createElement("title")

    title.appendChild(doc.createTextNode("learning math"))

    author=doc.createElement("author")

    author.appendChild(doc.createTextNode(u"张三"))

    pageNumber=doc.createElement("pageNumber")

    pageNumber.appendChild(doc.createTextNode("561"))

    book.appendChild(title)

    book.appendChild(author)

    book.appendChild(pageNumber)

    root.appendChild(book)

    doc.appendChild(root)

    #添加第二本书:

    book=doc.createElement("book")

    book.setAttribute("category","python")

    title=doc.createElement("title")

    title.appendChild(doc.createTextNode("learning python"))

    author=doc.createElement("author")

    author.appendChild(doc.createTextNode(u"李四"))

    pageNumber=doc.createElement("pageNumber")

    pageNumber.appendChild(doc.createTextNode("600"))

    book.appendChild(title)

    book.appendChild(author)

    book.appendChild(pageNumber)

    root.appendChild(book)

    print doc.toxml()

    #写入文件保存

    fp=codecs.open(r"e:abcook.xml","w","utf-8")

    doc.writexml(fp,indent="",addindent=" ",newl=" ",encoding="utf-8") fp.close()

    2.解析XML文件:

    from xml.dom.minidom import parse

    DOMTree=parse(r"e:abcook.xml")  #解析成内存中的一棵文档树

    print type(DOMTree)

    #print u"xml文档内容: %s"%DOMTree.toxml()

    booklist=DOMTree.documentElement  #获取文档树的根 print booklist

    #判断存在某属性并获取属性值

    if booklist.hasAttribute("type"):    

        print u"booklist元素存在type属性"    

        print u"booklist元素的type属性值是:%s"%booklist.getAttribute("type")

    else:    

        print u"booklist元素不存在type属性"

    #获取节点元素

    books=booklist.getElementsByTagName("book")

    print type(books)

    print books

    print u"book节点的个数:",len(books)

    print "*"*20

    for book in books:    

        if book.hasChildNodes():        

            #获取所有子节点对象        

            print book.childNodes

            #获取节点文本值        

            title=book.getElementsByTagName("title")        

            print "title is:",title[0].childNodes[0].data    

        else:        

            print u"不存在叶子节点"

  • 相关阅读:
    leetcode 190 Reverse Bits
    vs2010 单文档MFC 通过加载位图文件作为客户区背景
    leetcode 198 House Robber
    记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence
    逆序数2 HDOJ 1394 Minimum Inversion Number
    矩阵连乘积 ZOJ 1276 Optimal Array Multiplication Sequence
    递推DP URAL 1586 Threeprime Numbers
    递推DP URAL 1167 Bicolored Horses
    递推DP URAL 1017 Staircases
    01背包 URAL 1073 Square Country
  • 原文地址:https://www.cnblogs.com/reyinever/p/8654719.html
Copyright © 2011-2022 走看看