今天用到python解析xml文档,再改变数据之后,再保存会文件。

记录下代码:

import os,sys
from xml.dom.minidom import parse,Text
class DIYDom():
def __init__(self,path):
self.path
= path
self.dom
= self.toDOM(path)

def toDOM(self,path):
if os.path.isfile(path):
return parse(path)
return False

def getNode(self,uri,createFlag = False):
if uri[0] == "/":
uri
= uri[1:]
if uri[-1] == "/":
uri
= uri[:-1]
path
= uri.split("/")
node
= self.dom
existFlag
= False
for p in path:
for n in node.childNodes:
if n.nodeName == p:
node
= n
existFlag
= True
break
if existFlag == False and createFlag == True:
node
= self.createNode(node,p)
if existFlag == False and createFlag == False:
return False
existFlag
= False
return node

def createNode(self,parentNode,nodeName):
return parentNode.appendChild(self.dom.createElement(nodeName))

def setText(self,uri,text,createFlag = False):
node
= self.getNode(uri,createFlag)
if node == False:
return False
t
= Text()
t.data
= text
if node.firstChild == None:
node.appendChild(t)
else:
node.replaceChild(t,node.firstChild)
return True

def toXML(self):
domString
= self.dom.toxml()
return domString

def saveXML(self):
xmlString
= self.toXML()
xmlFile
= open(self.path,"w")
try:
xmlFile.write(xmlString)
return True
finally:
xmlFile.close()


D
= DIYDom("XML_NAME.zone.xml")
##node = D.getNode("/zone/extension/file")
D.setText("/zone/extension/files","testFile",True)
D.setText(
"/zone/extension/file","testFile",True)
D.setText(
"/zone/extension/propertiesFile","testFile",True)
print D.toXML()

XML_NAME.zone.xml的内容为:

<name>name</name>
<extension>
<name>testName</name>
<file>testFile</file>
<propertiesFile>testProperties</propertiesFile>
</extension>

from xml.dom.minidom import parse,Text
class DIYDom():
    def __init__(self,path):
        self.path = path
        self.dom = self.toDOM(path)

    def toDOM(self,path):
        if os.path.isfile(path):
            return parse(path)
        return False

    def getNode(self,uri,createFlag = False):
        if uri[0] == "/":
            uri = uri[1:]
        if uri[-1] == "/":
            uri = uri[:-1]
        path = uri.split("/")
        node = self.dom
        existFlag = False
        for p in path:
            for n in node.childNodes:
                if n.nodeName == p:
                    node = n
                    existFlag = True
                    break
            if existFlag == False and createFlag == True:
                node = self.createNode(node,p)
            if existFlag == False and createFlag == False:
                return False
            existFlag = False
        return node

    def createNode(self,parentNode,nodeName):
        return parentNode.appendChild(self.dom.createElement(nodeName))

    def setText(self,uri,text,createFlag = False):
        node = self.getNode(uri,createFlag)   
        if node == False:
            return False
        t = Text()
        t.data = text
        if node.firstChild == None:
            node.appendChild(t)
        else:
            node.replaceChild(t,node.firstChild)
        return True

    def toXML(self):
        domString = self.dom.toxml()
        return domString

    def saveXML(self):
        xmlString = self.toXML()
        xmlFile = open(self.path,"w")
        try:
            xmlFile.write(xmlString)
            return True
        finally:
            xmlFile.close()


D = DIYDom("XML_NAME.zone.xml")
##node = D.getNode("/zone/extension/file")
D.setText("/zone/extension/files","testFile",True)
D.setText("/zone/extension/file","testFile",True)
D.setText("/zone/extension/propertiesFile","testFile",True)
print D.toXML()
posted on 2011-02-18 16:29  Junw_china  阅读(2598)  评论(0编辑  收藏  举报