Hi, I'm writing a quick and dirty RSS feed aggregator and I'm getting very frustrated with one tiny problem. Occasionally the RSS XML docs contain characters which cause XML::Parser to choke.
The XML::Parser error message generated is:
"not well-formed (invalid token) at line 15, column 19, byte 530"
when I look at byte 530 it turns out to be the 'é' in Nescafé. Other exotic characters also cause XML::Parser to stop dead. I've tried the nice_string function from the Unicode man page:
sub nice_string {
join("",
map { $_ > 255 ? # if wide character...
sprintf("\\x{%04X}", $_) : # \x{...}
chr($_) =~ /[[:cntrl:]]/ ? # if control character
sprintf("\\x%02X", $_) : # \x..
chr($_) # else as themselves
} unpack("U*", $_[0])); # unpack Unicode
}
but this enrages XML::Parser even further and it fails and the first end-of-line character. I'm using LWP::Simple to grab the XML so my script essentially looks like this:
my $rss = new XML::RSS;
eval { $rss->parse(nice_string(get($url))); };
Can anyone recomend a module/function that will reliably sanitise the string that get() returns, in an encoding suitable for XML::Parser?
PS I've looked at the 'encoding' option for XML::Parser but it doesn't seem to change the result :(
In reply to XML::RSS
by alexg
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.