如何解决将 ElementTree 转换为字符串会从输出中去除 CDATA 部分
我有一个包含上述代码片段的 XML。这里我需要将 "LotContext id="ABC12345"" 更改为不同的值并输出 XML。
<BatchContextList>
<BatchContext id="">
<BatchPropertyList />
<ContextList>
<LotContext id="ABC12345">
<PropertyList>
<LotProperty Name="LP1"><![CDATA[true]]></LotProperty>
<LotProperty Name="LP2"><![CDATA[1000]]></LotProperty>
<LotProperty Name="LP3"><![CDATA[1]]></LotProperty>
<LotProperty Name="LP4"><![CDATA[1:1000;]]></LotProperty>
</PropertyList>
<LotEntityContextList />
</LotContext>
</ContextList>
</BatchContext>
</BatchContextList>
我成功识别了节点并更改了值,但在输出 XML 中,CDATA 部分被剥离或数据错误。
例如
<EntityProperty Name="RegisteredFeature"><![CDATA[<Features><Feature capabilityName="FORM"
输出如下。
<EntityProperty Name="RegisteredFeature"><Features><Feature capabilityName="FORM"
下面是我使用的代码
faimXmlTree= ET.parse(faimXml)
rootfaimXmlTree = faimXmlTree.getroot()
unitDatas = rootfaimXmlTree.findall(".//LotContext")
unitDatas[0].attrib['id'] = "newValue"
xmlstr=ET.tostring(rootfaimXmlTree,method='xml').decode('utf-8')
print(xmlstr)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。