【发布时间】:2014-12-27 23:52:36
【问题描述】:
我使用此代码创建一个具有预期输出的新节点:
<item desc="desc foobar"><![CDATA[qux]]></item>
代码:
open my $fh, "<", $xml_file;
binmode $fh;
my $parser = XML::LibXML->new();
my $doc = $parser->load_xml(IO => $fh);
# create a new node in XML file
my $root = $doc->getDocumentElement();
my $new_element = $doc->createElement("item");
# FIXME
$new_element->appendTextNode(sprintf '<![CDATA[%s]]>', join "\n", @input);
$new_element->setAttribute('desc', $desc);
$root->appendChild($new_element);
close $fh;
open my $out, '>', $xml_file;
binmode $out;
$doc->toFH($out);
close $out;
它可以很好地创建新元素文本,但我想知道如何在没有 XML 实体替换的情况下添加 CDATA:我明白了:
<item desc="dddd"><![CDATA[qux]]>
# ^^^^
【问题讨论】:
标签: xml perl xml-libxml