jbcedge
jbcedge

Reputation: 19505

What is the correct way to use escape_value() in XML::Simple

Can anyone please guide me how to use this escape_value(). I want to escape this characters:  because it gives reference to invalid character number error.

this is my code

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);

thanks in advance

Upvotes: 0

Views: 738

Answers (2)

ikegami
ikegami

Reputation: 385897

U+000C is not allowed in XML.

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

Not even escaped.

Characters referred to using character references MUST match the production for Char.

Upvotes: 1

David W.
David W.

Reputation: 107040

Looking at the code, it appears that the escape_value method takes a string and returns returns that string with the following substitutions:

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

It doesn't even manipulate the structure of the object. If you call it directly, you simply would do this:

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

Heck, why even bother with object orientation?

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

What it looks like is that this subroutine is called by XMLOut itself whenever the option NoEscape is either not set or set to 0.

In line 636 of the code, the XMLOut method is calling the value_to_xml method. This takes the data structure passed into XMLOut and creates an XML text. The value_to_xml method then calls the escape_string method in line 1431.

Upvotes: 1

Related Questions