Python minidom:可以避免丢弃元数据,空格?

时间:2014-08-22 18:39:01

标签: python xml

我正在使用python模块xml.dom.minidom编辑XML。问题如下。当我使用writexml更新我的XML文件时,它会从程序中删除空格,并且还会删除一些数据,例如它会自动从standalone="yes"中删除<?xml version="1.0" standalone="yes"?>

请帮我解决这个问题。这是我的代码:

from xml.dom.minidom import parse
import xml.dom.minidom

file_handle = open(r'c:\users\g\desktop\filename.xml',"wb")
DOMTree = xml.dom.minidom.parse(r'c:\users\g\desktop\IT_ADMINVersionTrack.xml')
collection = DOMTree.documentElement
movies = collection.getElementsByTagName("dtModule")

for movie in movies:
   type2 = movie.getElementsByTagName('FolderName')[0]
   print "Folder: %s" % type2.childNodes[0].data
   type = movie.getElementsByTagName('Version')[0]
   print "VERSION: %s" % type.childNodes[0].data
   type = movie.getElementsByTagName('Version')[0]
   type.childNodes[0].data = "12.15.0"
   print "NEW VERSION: %s" % type.childNodes[0].data

DOMTree.writexml(file_handle)

0 个答案:

没有答案