Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Greeting fellow monks,

For the better part of this past year, I've been the principal engineer at my company on a project involving handling XML data.  I chose to use XML::Simple (with XML::LibXML::SAX for the parser), and it has served my needs quite well, even though it was my first real experience with XML processing.

Along the way, I've needed to massage input text so that XML::Simple would handle it correctly.  The XML is always output as "UTF-8", and the subroutine I was using for "cleaning" the input text looked like this:

sub xml_compliance { my ($self, $field) = @_; $field =~ s/&/&amp;/g; $field =~ s/</&lt;/g; $field =~ s/>/&gt;/g; $field =~ s/"/&quot;/g; $field =~ s/'/&#39;/g; return $field; }

Yesterday we discovered that this wasn't sufficient; an error occurred because someone gave us input containing a character which looked like an apostrophe (ascii 0x27), but was in fact an ascii 0x92 ("smart quotes", or whatever they're called), which broke the parsing.  When I say "broke the parsing", I mean that I this error from XMLin:

Entity: line 15: parser error : Input is not proper UTF-8, indicat +e encoding !

So I revised my subroutine as follows:

sub xml_compliance { my ($self, $field) = @_; my $fixed = ""; my $h_translate = { '&' => '&amp;', '<' => '&lt;', '>' => '&gt;', '"' => '&quot;', "'" => '&#39;', }; my @chars = split //, $field; foreach my $char (@chars) { if (exists $h_translate->{$char}) { $fixed .= $h_translate->{$char}; } elsif (ord($char) > 0x7f) { # This is where we handle all non 7-bit ascii $fixed .= sprintf "&#%02x;", ord($char); } else { $fixed .= $char; } } return $fixed; }

That is, it will handle those "smart quotes" by converting them to "&#92;", as well as any other ascii characters with the high bit turned on.

My questions are:

  1. Is this an appropriate way to handle such characters?
  2. Is there some simpler/easier/better way to do it?
  3. Is there some module for this which I'm unaware of?
  4. Is there anything else I should be taking into account?

Thanks in advance for any enlightenment!


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

In reply to Cleaning up non 7-bit Ascii Chars for XML-processing by liverpole

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-24 01:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found