in reply to Data Exchange between perl and java.

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

Replies are listed 'Best First'.
Re: Re: Data Exchange between perl and java.
by GermanHerman (Sexton) on Aug 15, 2003 at 22:13 UTC
    Perfect!! Thank you so much Inman that is exactly what I
    needed. You get TWO gold stars (and a vote to boot)
    -Douglas