pythonmidom:可以避免丢弃元数据、空格吗?

2024-10-06 12:37:55 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: importdatatypexmlmoviedomyesstandalone