How to replace the full content of a node using any xml lib?
I'm trying to parse a xml file using Python but I have some tags that might contain xml data. For example:
<code>
<?xml version="1.0" encoding="utf-8"?>
</code>
I want to write CDATA tags like this:
<code><![CDATA[
<?xml version="1.0" encoding="utf-8"?>
]]>
</code>
开发者_高级运维I tried (with lxml):
a = etree.fromstring(data)
for e in a.findall("code"):
e.text = etree.CDATA(etree.tostring(e))
But I get:
<code><![CDATA[<code><?xml version="1.0" encoding="utf-8"?></code>]]>
<?xml version="1.0" encoding="utf-8"?>
</code>
Your replacement code finds the <code>
element and serializes it as the new text of the element. It seems like you want to include only the children of the <code>
element, though.
Try setting e.text
to CDATA(e.text)
instead of to the result of serializing e
.
(I'm the OP i registered after posting)
With e.text = etree.CDATA(e.text)
I get
<code type="php"><![CDATA[
]]><?php echo $foo;
?>
</code>
for
<code type="php">
<?php
echo $foo;
?>
</code>
精彩评论