加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用XML :: LibXML appendTextNode()AS-IS附加CDATA

发布时间:2020-12-16 23:12:19 所属栏目:百科 来源:网络整理
导读:我使用此代码创建具有预期输出的新节点: 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 filemy $root = $do
我使用此代码创建具有预期输出的新节点:

<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">&lt;![CDATA[qux]]>
#                 ^^^^

解决方法

很明显,appendTextNode()会自动转义文本节点中有问题的字符.这是你应该做的:

my $cdata_node = XML::LibXML::CDATASection->new(
    join "n",@input
);

$new_element->appendChild($cdata_node);

XML ::的libxml ::的CDATASection

$node = XML::LibXML::CDATASection->new( $content );

The constructor is the only provided function for this package. It is
required,because libxml2 treats the different text node types
slightly differently.

The class inherits from XML::LibXML::Node

http://search.cpan.org/dist/XML-LibXML/lib/XML/LibXML/CDATASection.pod

XML ::的libxml ::节点

$childnode = $node->appendChild( $childnode );

The function will add the $childnode to the end of $node’s children…

http://search.cpan.org/~shlomif/XML-LibXML-2.0117/lib/XML/LibXML/Node.pod

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读