I've written an application which parses RSS feeds and outputs a Web page so I can track changes on my favourite sites. There's nothing original or exciting about that.

I discovered that several sites I'm interested in sometimes output badly formed RSS. Special characters are often unescaped, which causes problems for XML::RSS and any other strict XML parser.

I've written a simple heuristic to parse badly formed XML, which might be of use to others. Maybe you can help me improve this?

use XML::RSS (); my $rss_content = "RSS goes here"; my @replace = ( ['\&(\s)', '"&$1"'], # '&' followed by space ['(\s)>', '"$1>"'], # '>' with preceding space ['\&', '"&"'], # All '&' ); my($data, $rss); PARSE: while (my $repl = shift @replace) { $rss = XML::RSS->new; $data = eval { $rss->parse($rss_content) } and last PARSE; } continue { $content =~ s/$repl->[0]/eval($repl->[1])/ge; } die unless $data;

I've only used this with XML::RSS, but this technique could be applied to XML::Parser, or any other XML parsing code.


In reply to Parsing badly formed RSS or XML by tomhukins

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.