(<, >, &) are encoded by XML::Writer, however (', ") are not. I need to encode both (', ") and I suspect my current solution is not very good. I have essentially made a placeholder for each character, and, after XML::Writer has rendered the XML I'm running a global search replace so I end of with the desired output.

I should add that if I encode these characters before outputting with XML::Writer I get:

<root> <str1>one&lt;two</str1> <str2>one&amp;two</str2> <str3>two&gt;one</str3> <str4>Caisse D&amp;quot;Eparge</str4> <str5>Caisse D&amp;apos;Eparge</str5> </root>

The desired output is:

<root> <str1>one&lt;two</str1> <str2>one&amp;two</str2> <str3>two&gt;one</str3> <str4>Caisse D&quot;Eparge</str4> <str5>Caisse D&apos;Eparge</str5> </root>

Program

use strict; use warnings; use Cwd; use XML::Writer; use XML::Writer::String; use HTML::Entities; use utf8; use Modern::Perl; my $str1 = qq(one<two); # &lt; (<) my $str2 = qq(one&two); # &amp; (&) my $str3 = qq(two>one); # &gt; (>) my $str4 = qq(Caisse D"Eparge); # &quot; ("); my $str5 = qq(Caisse D'Eparge); # &apos; ('); $str4 =~ s/\x{22}/\#\#\#doublequote\#\#\#/g; $str5 =~ s/\x{27}/\#\#\#apostrophe\#\#\#/g; say "str4: $str4"; say "str5: $str5"; my $file = cwd() . '/test.xml'; my $BOD = XML::Writer::String->new(); my $Writer = XML::Writer->new( OUTPUT => $BOD, DATA_MODE => 1, DATA_INDENT => 2 ); $Writer->xmlDecl("UTF-8"); $Writer->startTag('root'); $Writer->dataElement('str1', $str1); $Writer->dataElement('str2', $str2); $Writer->dataElement('str3', $str3); $Writer->dataElement('str4', $str4); $Writer->dataElement('str5', $str5); $Writer->endTag('root'); $Writer->end(); my $xml = $BOD->value(); $xml =~ s/\#\#\#doublequote\#\#\#/\&quot\;/g; $xml =~ s/\#\#\#apostrophe\#\#\#/\&apos\;/g; my $fh = new IO::File "> $file"; if (defined $fh) { print $fh "$xml"; $fh->close; } exit;

In reply to How to encode apostrophe and quote using XML::Writer? by bcurrens

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.