使用Python编写新值后,Python如何保持XML注释存在? [英] Python how to keep the XML comment exist after write a new value using Python?

查看:58
本文介绍了使用Python编写新值后,Python如何保持XML注释存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文件,然后需要更新一些值.我的XML文件包含注释.在编写XML之后,我想保留注释,但是它消失了.

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.

这是我的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文件的输出变为this.注释和<?xml version ="1.0"编码="UTF-8"?消失了.

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.

推荐答案

  1. 使用 ET.XMLParser 保留注释
  2. write()函数中使用 encoding xml_declaration 参数编写xml声明尝试以下代码:
  1. Use ET.XMLParser to preserve comments
  2. Use encoding and xml_declaration arguments in write() 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)

您还可以避免使用变量 read ,而只需使用 tree :

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)

这篇关于使用Python编写新值后,Python如何保持XML注释存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆