在XML :: Simple中使用escape_value()的正确方法是什么

时间:2013-07-02 22:39:05

标签: xml perl

任何人都可以指导我如何使用这个escape_value()。 我想转义这些字符:因为它引用了无效的字符数错误。

这是我的代码

my $xs = XML::Simple->new( KeepRoot => 1, KeyAttr => 1, ForceArray => 1 );
$xs->XMLout($filename, escape_value(`'`')); // but this is not working
my $data = $xs->XMLin($filename);

提前致谢

2 个答案:

答案 0 :(得分:1)

查看code,似乎escape_value方法接受一个字符串并返回该字符串,并返回以下内容:

  • >>
  • <&lt;
  • &&amp;
  • "&quot;

它甚至不操纵对象的结构。如果你直接调用它,你只需这样做:

my $string = '"Remove quotes" & <entities> quotes';
my $encoded_string = $self->escape_value( "$string" );
say $encoded_string;  #&quot;Remove quotes&quot; &amp; and &lt;entities&gt; quotes

哎呀,为什么还要为面向对象而烦恼?

my $string = '"Remove quotes" & <entities> quotes';
my $encoded_string = XML::Simple::escape_value( "$string" );
say $encoded_string;  #&quot;Remove quotes&quot; &amp; and &lt;entities&gt; quote

如果选项XMLOut未设置或设置为NoEscape,则0本身会调用此子例程。

在代码的第636行中,XMLOut方法正在调用value_to_xml方法。这将数据结构传递到XMLOut并创建XML文本。然后,value_to_xml方法会在第1431行调用escape_string方法。

答案 1 :(得分:1)

XML中不允许使用U + 000C。

  

Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]

甚至没有逃脱。

  

使用字符引用引用的字符必须与Char的制作匹配。