I did some work using XML::Writer to create XML docs from fielded text file. I noticed that it only escaped four or five 'special' chars. e.g. '&' to &.

This was OK until I started encountering some data which was outside the 7bit ascii range that was not escaped and throwing some XML parsing tools. I extended the code to do a general escape of characters outside the 7bit range.

You could use a similar approach if you find that unescaped chars are causing a problem.

sub XML::Writer::_escapeLiteral { # escape 'normal' characters e.g. ampersand if ($_[0] =~ /[<>&"']/) { $_[0] =~ s/\&/&amp;/g; # ampersand $_[0] =~ s/\"/&quot;/g; # quotes $_[0] =~ s/\</&lt;/g; # left angle $_[0] =~ s/\>/&gt;/g; # right angle $_[0] =~ s/\'/&apos;/g; # apostrophe # Add more here... } # Look for occurrances of chars in the range outside normal 7 bit +ascii table # Note the character in Octal if ($_[0] =~ /([\177-\377])/) { my $escapedString = ""; # Generic escape for all other chars while ($_[0] =~ /\G(.*?)([\177-\377])/gc) { # concat the escaped char in the form &#123; $escapedString .= sprintf ("%s&#%d;", $1, ord($2)); #print "escapedstring = $1 \n"; } # Concat whatever is left if ($_[0] =~ /\G(.*)$/g) { $escapedString .= $1; } return $escapedString; } return $_[0]; }

Enjoy!
Inman


In reply to Re: Data Exchange between perl and java. by inman
in thread Data Exchange between perl and java. by GermanHerman

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.