繁体   English   中英

即使在使用strip_cdata = False之后CDATA也被剥离了lxml

[英]CDATA getting stripped in lxml even after using strip_cdata=False

我有一个要求,我需要读取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")

以下是Sample 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>&lt;p&gt;Moto Vblock System 300 Release 4.0.21&lt;/p&gt;
&lt;ul&gt;&lt;li&gt; TMM VNX OE for File was updated to 7.1.79.8.&lt;/li&gt;
&lt;/ul&gt;
</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。

文件说明

请注意.text属性如何不表示文本内容由CDATA部分包装。 如果要确保数据由CDATA块包装,可以使用CDATA()文本包装器。

因此,如果要保留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内容在.text属性中连接并作为str ),因此无法确定CDATA最初是否使用过。 (请参阅确定CDATA在lxml元素中的位置?源代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM