Python 如何在使用 Python 写入新值后保持 XML 注释存在?
Python how to keep the XML comment exist after write a new value using Python?
我有一个 XML 文件然后需要更新一些值。我的 XML 文件包含评论。写完XML本想保留评论,结果没了
这是我的 XML:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Data System -->
<process>
<!-- Student Name -->
<NAME source="hsfg" class="hshah" property="Name">
<VALUE type="string"></VALUE>
<VALUE type="None"></VALUE>
</NAME>
<!-- Exercise Number -->
<number source="hsfg" class="hgdgf" property="gagfa">
<VALUE type="string"></VALUE>
<VALUE type="None"></VALUE>
</number>
<!-- Exam ID -->
<id source="hsfg" class="gfdg" property="fadg">
<VALUE type="string"></VALUE>
<VALUE type="None"></VALUE>
</id>
</process>
这是我需要更新的值
<!-- Student Name -->
<NAME source="hsfg" class="hshah" property="Name">
<VALUE type="string"></VALUE>
<VALUE type="None"></VALUE>
</NAME>
变成这样:
<!-- Student Name -->
<NAME source="hsfg" class="hshah" property="Name">
<VALUE type="string">new value</VALUE>
<VALUE type="None"></VALUE>
</NAME>
我的代码:
read = ET.parse('test.xml').getroot()
parent = read.find('.//NAME[@property="Name"]')
change = parent.find('VALUE[@type="string"]')
change.text = 'new value'
tree = ET.ElementTree(read)
tree.write("test.xml")
test.xml文件的输出变成这样。评论和 <?xml version="1.0" encoding="UTF-8"?>
消失了。
<process>
<NAME source="hsfg" class="hshah" property="Name">
<VALUE type="string">new value</VALUE>
<VALUE type="None" />
</NAME>
<number source="hsfg" class="hgdgf" property="gagfa">
<VALUE type="string" />
<VALUE type="None" />
</number>
<id source="hsfg" class="gfdg" property="fadg">
<VALUE type="string" />
<VALUE type="None" />
</id>
</process>
但我的期望输出、结构和注释仍然与之前相同,更新值如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Data System -->
<process>
<!-- Student Name -->
<NAME source="hsfg" class="hshah" property="Name">
<VALUE type="string">new value</VALUE>
<VALUE type="None"></VALUE>
</NAME>
<!-- Exercise Number -->
<number source="hsfg" class="hgdgf" property="gagfa">
<VALUE type="string"></VALUE>
<VALUE type="None"></VALUE>
</number>
<!-- Exam ID -->
<id source="hsfg" class="gfdg" property="fadg">
<VALUE type="string"></VALUE>
<VALUE type="None"></VALUE>
</id>
</process>
- 使用
ET.XMLParser
保留评论
- 在
write()
函数中使用encoding
和xml_declaration
参数编写xml声明
试试下面的代码:
import xml.etree.ElementTree as ET
parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True))
read = ET.parse('newXml.xml', parser=parser).getroot()
parent = read.find('.//NAME[@property="Name"]')
change = parent.find('VALUE[@type="string"]')
change.text = 'new value'
tree = ET.ElementTree(read)
tree.write("test.xml", encoding='utf-8', xml_declaration=True)
你也可以避免使用变量 read
而只使用 tree
:
import xml.etree.ElementTree as ET
parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True))
tree = ET.parse('newXml.xml', parser=parser)
parent = tree.find('.//NAME[@property="Name"]')
change = parent.find('VALUE[@type="string"]')
change.text = 'new value'
tree.write("test.xml", encoding='utf-8', xml_declaration=True)
我有一个 XML 文件然后需要更新一些值。我的 XML 文件包含评论。写完XML本想保留评论,结果没了
这是我的 XML:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Data System -->
<process>
<!-- Student Name -->
<NAME source="hsfg" class="hshah" property="Name">
<VALUE type="string"></VALUE>
<VALUE type="None"></VALUE>
</NAME>
<!-- Exercise Number -->
<number source="hsfg" class="hgdgf" property="gagfa">
<VALUE type="string"></VALUE>
<VALUE type="None"></VALUE>
</number>
<!-- Exam ID -->
<id source="hsfg" class="gfdg" property="fadg">
<VALUE type="string"></VALUE>
<VALUE type="None"></VALUE>
</id>
</process>
这是我需要更新的值
<!-- Student Name -->
<NAME source="hsfg" class="hshah" property="Name">
<VALUE type="string"></VALUE>
<VALUE type="None"></VALUE>
</NAME>
变成这样:
<!-- Student Name -->
<NAME source="hsfg" class="hshah" property="Name">
<VALUE type="string">new value</VALUE>
<VALUE type="None"></VALUE>
</NAME>
我的代码:
read = ET.parse('test.xml').getroot()
parent = read.find('.//NAME[@property="Name"]')
change = parent.find('VALUE[@type="string"]')
change.text = 'new value'
tree = ET.ElementTree(read)
tree.write("test.xml")
test.xml文件的输出变成这样。评论和 <?xml version="1.0" encoding="UTF-8"?>
消失了。
<process>
<NAME source="hsfg" class="hshah" property="Name">
<VALUE type="string">new value</VALUE>
<VALUE type="None" />
</NAME>
<number source="hsfg" class="hgdgf" property="gagfa">
<VALUE type="string" />
<VALUE type="None" />
</number>
<id source="hsfg" class="gfdg" property="fadg">
<VALUE type="string" />
<VALUE type="None" />
</id>
</process>
但我的期望输出、结构和注释仍然与之前相同,更新值如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Data System -->
<process>
<!-- Student Name -->
<NAME source="hsfg" class="hshah" property="Name">
<VALUE type="string">new value</VALUE>
<VALUE type="None"></VALUE>
</NAME>
<!-- Exercise Number -->
<number source="hsfg" class="hgdgf" property="gagfa">
<VALUE type="string"></VALUE>
<VALUE type="None"></VALUE>
</number>
<!-- Exam ID -->
<id source="hsfg" class="gfdg" property="fadg">
<VALUE type="string"></VALUE>
<VALUE type="None"></VALUE>
</id>
</process>
- 使用
ET.XMLParser
保留评论 - 在
write()
函数中使用encoding
和xml_declaration
参数编写xml声明 试试下面的代码:
import xml.etree.ElementTree as ET
parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True))
read = ET.parse('newXml.xml', parser=parser).getroot()
parent = read.find('.//NAME[@property="Name"]')
change = parent.find('VALUE[@type="string"]')
change.text = 'new value'
tree = ET.ElementTree(read)
tree.write("test.xml", encoding='utf-8', xml_declaration=True)
你也可以避免使用变量 read
而只使用 tree
:
import xml.etree.ElementTree as ET
parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True))
tree = ET.parse('newXml.xml', parser=parser)
parent = tree.find('.//NAME[@property="Name"]')
change = parent.find('VALUE[@type="string"]')
change.text = 'new value'
tree.write("test.xml", encoding='utf-8', xml_declaration=True)