我有一个要求,我需要读取一个 XML 文件并用某个值替换一个字符串。 XML 包含 CDATA 元素,我需要保留它。
我曾尝试使用解析器并将 strip_data 设置为 false。这是行不通的,需要帮助才能找到实现它的方法。
import lxml.etree as ET
parser1 = ET.XMLParser(strip_cdata=False)
with open('testxml.xml', encoding="utf8") as f:
tree = ET.parse(f, parser=parser1)
root = tree.getroot()
for elem in root.getiterator():
try:
elem.text = elem.text.replace('Bundled Manager 2.2(8b)', '123456')
except AttributeError:
pass
tree.write('output_new8.xml', xml_declaration=True, method='xml', encoding="utf8")
以下是示例 xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><!-- Copyright (c) 2015 Moto Company, LLC. All rights reserved. Moto Confidential/Proprietary Information -->
<Benchmark>
<status date="2013-03-11">draft</status>
<title>Logitech TMM block(TM) System 300 Release Certification Matrix</title>
<description>Random discription</description>
<version time="2013-03-05T15:20:20.995-04:00" update="">3.0.0-2017.03.00</version>
<model system="urn:xccdf:scoring:default"/>
<Profile id="xccdf_com.Moto_profile_release_4.0.21">
<status date="2016-03-30">draft</status>
<title>RCM 4.0.21</title>
<description><![CDATA[<p>Moto Vblock System 300 Release 4.0.21</p>
<ul><li> TMM VNX OE for File was updated to 7.1.79.8.</li>
</ul>]]>
</description>
<set-value idref="xccdf_com.Moto_value_vision_content_version">3.0.0-2015.07.00</set-value>
<set-value idref="xccdf_com.Moto_value_vision_version">3.0.0</set-value>
<set-value idref="xccdf_com.Moto_value_vplex_version">5.3.0.03.00.04</set-value>
<set-value idref="xccdf_com.Moto_value_powerpath_version">Bundled Manager 2.2(8b)</set-value>
<select idref="xccdf_com.Moto_rule_vnx_version" selected="true"/>
<select idref="xccdf_com.Moto_rule_vplex_version" selected="true"/>
</Profile>
</Benchmark>
代码的输出如下所示:
<?xml version='1.0' encoding='UTF8'?>
<!-- Copyright (c) 2015 Moto Company, LLC. All rights reserved. Moto Confidential/Proprietary Information --><Benchmark>
<status date="2013-03-11">draft</status>
<title>Logitech TMM block(TM) System 300 Release Certification Matrix</title>
<description>Random discription</description>
<version time="2013-03-05T15:20:20.995-04:00" update="">3.0.0-2017.03.00</version>
<model system="urn:xccdf:scoring:default"/>
<Profile id="xccdf_com.Moto_profile_release_4.0.21">
<status date="2016-03-30">draft</status>
<title>RCM 4.0.21</title>
<description><p>Moto Vblock System 300 Release 4.0.21</p>
<ul><li> TMM VNX OE for File was updated to 7.1.79.8.</li>
</ul>
</description>
<set-value idref="xccdf_com.Moto_value_vision_content_version">3.0.0-2015.07.00</set-value>
<set-value idref="xccdf_com.Moto_value_vision_version">3.0.0</set-value>
<set-value idref="xccdf_com.Moto_value_vplex_version">5.3.0.03.00.04</set-value>
<set-value idref="xccdf_com.Moto_value_powerpath_version">123456</set-value>
<select idref="xccdf_com.Moto_rule_vnx_version" selected="true"/>
<select idref="xccdf_com.Moto_rule_vplex_version" selected="true"/>
</Profile>
</Benchmark
>
如您所见,CDATA 部分已被剥离。
如果有人能在这里帮助我,那就太好了。
最佳答案
这是因为你在做
elem.text = elem.text.replace('Bundled Manager 2.2(8b)', '123456')
它用普通文本节点替换 CDATA。
documentation 状态
因此,如果你想保留 CDATA 部分,你应该只在修改它时分配给
elem.text
,并指示 lxml 使用 CDATA 部分:if 'Bundled Manager 2.2(8b)' in elem.text:
elem.text = ET.CDATA(elem.text.replace('Bundled Manager 2.2(8b)', '123456'))
由于
ElementTree
库的工作方式(整个文本和 cdata 内容在 str
属性中连接并作为 .text
公开),实际上不可能知道 CDATA 是否最初被使用。 (见 Figuring out where CDATA is in lxml element? 和 the source code )关于python - 即使在使用 strip_cdata=False 后,CDATA 也会在 lxml 中被剥离,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44549514/