zoukankan      html  css  js  c++  java
  • Python文件之----XML

     1 #coding=utf-8
     2 from xml.dom import minidom
     3 from xml.dom.minidom import Document
     4 import xml
     5 def writeXML(filaName="test.xml"):
     6    doc = Document()
     7    feature=doc.createElement("feature")
     8    doc.appendChild(feature)
     9    father=doc.createElement("father")
    10    father.setAttribute('name','noun') #元素属性
    11    text = doc.createTextNode('系统')#元素值
    12 
    13    feature.appendChild(father)
    14    father.appendChild(text)
    15    son=doc.createElement("son")
    16    text = doc.createTextNode('系统')#元素值
    17    son.appendChild(text)
    18    father.appendChild(son)
    19    f = open(filaName,'w')
    20    f.write(doc.toprettyxml(indent = ''))
    21    f.close()
    22 def readXML(fileName="test.xml"):
    23    dom = xml.dom.minidom.parse(fileName) #打开xml文档 
    24    root = dom.documentElement #得到文档元素对象
    25    bb = root.getElementsByTagName('father')
    26    b=bb[0]
    27    print b.nodeName
    28    print b.nodeValue
    29    print b.nodeType
    30    print b.getAttribute("name")
    31    print b.firstChild.data.encode("utf-8")
    32 def main():
    33     writeXML()
    34     # readXML()
    35 if __name__=="__main__":
    36     main()

    写入的xml文档内容:

    <?xml version="1.0" ?>
    <feature>
    <father name="noun">
    系统
    <son>系统</son>
    </father>
    </feature>

    可能写入的xml文档格式不是很好看,显示父子关系不好,可通过文本写入的方式,调整xml的格式。

    对于xml的每个节点有三种属性:

    nodeName为结点名字。

    nodeValue是结点的值,只对文本结点有效。

    nodeType是结点的类型。catalog是ELEMENT_NODE类型

    第一个系统是father的标签之间的数据。

     1 #coding=utf-8
     2 from xml.dom import minidom
     3 from xml.dom.minidom import Document
     4 import xml
     5 def writeXML(filaName="test.xml"):
     6    doc = Document()
     7    feature=doc.createElement("feature")
     8    doc.appendChild(feature)
     9    father=doc.createElement("father")
    10    father.setAttribute('name','noun') #元素属性
    11    text = doc.createTextNode('系统')#元素值
    12 
    13    feature.appendChild(father)
    14    father.appendChild(text)
    15    son=doc.createElement("son")
    16    text = doc.createTextNode('系统')#元素值
    17    son.appendChild(text)
    18    father.appendChild(son)
    19    f = open(filaName,'w')
    20    f.write(doc.toprettyxml(indent = ''))
    21    f.close()
    22 def readXML(fileName="test.xml"):
    23    dom = xml.dom.minidom.parse(fileName) #打开xml文档 
    24    root = dom.documentElement #得到文档元素对象
    25    bb = root.getElementsByTagName('father')
    26    b=bb[0]
    27    print b.nodeName
    28    print b.nodeValue
    29    print b.nodeType
    30    print b.getAttribute("name")
    31    print b.firstChild.data.encode("utf-8")
    32 def main():
    33     # writeXML()
    34     readXML()
    35 if __name__=="__main__":
    36     main()
    37 '''
    38 输出:
    39 father
    40 None
    41 1
    42 noun
    43 系统
    44 [Finished in 0.1s]
    45 '''
  • 相关阅读:
    浅谈IIS处理Asp.net请求和 Asp.net页面生命周期
    Windows Phone 7与Android和iPhone的比较
    ExtJs开发需要引用的Js库文件
    Windows phone开发(1)一些基本概念
    synthesize
    Objective C 基础教程
    Objective C基础教程笔记
    ObjectiveC语法与Cocoa框架
    Handing basic Interactio IOS notes
    Learning ObjectiveC and Xcode
  • 原文地址:https://www.cnblogs.com/moye13/p/4545415.html
Copyright © 2011-2022 走看看