python - Python how to keep the XML comment exist after write a new value using Python?
问题描述
I have an XML file then need to update some value. My XML file contains the comment. I would like to keep the comment after writing the XML, but it disappeared.
Here is my 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>
This is the value that I need to update
<!-- Student Name -->
<NAME source="hsfg" class="hshah" property="Name">
<VALUE type="string"></VALUE>
<VALUE type="None"></VALUE>
</NAME>
become this:
<!-- Student Name -->
<NAME source="hsfg" class="hshah" property="Name">
<VALUE type="string">new value</VALUE>
<VALUE type="None"></VALUE>
</NAME>
My code:
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")
The output of the test.xml file become this. the comment and <?xml version="1.0" encoding="UTF-8"?>
dissapeared.
<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>
BUT my expectation output, the structure and the comment still the same as before update the value like this:
<?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>
Anyone can help me, please. Thank you so much.
解决方案
- Use
ET.XMLParser
to preserve comments - Use
encoding
andxml_declaration
arguments inwrite()
function to write xml declaration Try the following code:
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)
you can also avoid variable read
and just use 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)
推荐阅读
- unity3d - 使用 Unity 3D 移动鼠标时相机跟随玩家
- javascript - Blaze 模板,遍历字段?
- reactjs - 为什么“禁用:”顺风前缀在我的反应应用程序中不起作用?
- python - Pygame 在点击之间重置计时器
- routes - 在 Raspberry Pi 4 上添加静态 ip 路由/规则
- javascript - 使用 AJAX(JavaScript 和 PHP)发送邮件表单不起作用
- javascript - NodeJS http 服务器指标
- python - 计算 pandas 数据帧中的重叠时间帧,按人分组
- spring-boot - 在 JPA 应用程序中检索 Nextval 时出现问题
- java - 在我的 sqlite 表中,如何使用电话号码作为键来查找特定金额?