htmanning has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to read a txt file with contents that look like this:
<LI><A HREF="http://www.mysite.com/cgi-bin/viewnews.cgi?category=1&id= +1265624320"><FONT COLOR="#000000" SIZE="-1" FACE="arial">Test headlin +e </FONT></A></FONT><BR> <LI><A HREF="http://www.mysite.com/cgi-bin/viewnews.cgi?category=1&id= +1264624826"><FONT COLOR="#000000" SIZE="-1" FACE="arial">Test headlin +e 2</FONT></A></FONT><BR> <LI><A HREF="http://www.mysite.com/cgi-bin/viewnews.cgi?category=1&id= +126j624329"><FONT COLOR="#000000" SIZE="-1" FACE="arial">Test headlin +e 3</FONT></A></FONT><BR>
I'm trying to write a bare bones xml feed with what I read in, after stripping some tags. I use this:
#!/usr/local/bin/perl use LWP::Simple; #################### # STATIC VARIABLES # #################### $original_include = "/usr/www/users/me/articles/1view.h"; $outfile = "/usr/www/users/me/test.rss"; open (FILE, $original_include) || die "couldn't open the file!"; @news = <FILE>; close(FILE); open(NEWSFEED, ">$outfile") || die "Cannot Open $outfile"; print NEWSFEED<<TEXT; <?xml version="1.0" encoding="iso-8859-1"?> <rss version="0.91"> <channel> <title>Test</title> <link>http://www.mysite.com</link> <description> Test </description> <language>en-us</language> <image> <title>Test</title> <url>http://www.mysite.com/</url> <link>http://www.mysite.com/icon.jpg</link> </image> TEXT foreach $news_include (@news) { $news_include =~ s/<LI>/<item><link>/gi; $news_include =~ s/<FONT COLOR=\"\#000000\" SIZE=\"-1\" FACE=\"arial\" +>/<\/link><title>/gi; $news_include =~ s/<\/FONT><\/A><\/FONT><BR>/<\/title><\/item>/gi; print NEWSFEED<<TEXT; $news_include TEXT } #end foreach $news_include (@news_include) print NEWSFEED"</channel>"; print NEWSFEED"</rss>"; close(NEWSFEED); system("chmod 0777 $outfile");
All that gets printed to the xml file is this:
<?xml version="1.0" encoding="iso-8859-1"?> <rss version="0.91"> <channel> <title>Test</title> <link>http://www.mysite.com</link> <description> Test </description> <language>en-us</language> <image> <title>Test</title> <url>http://www.mysite.com/</url> <link>http://www.mysite.com/icon.jpg</link> </image>
I'm sure I'm missing something here.

Replies are listed 'Best First'.
Re: Help reading file, stripping tags, writing new file
by aquarium (Curate) on Feb 17, 2010 at 01:31 UTC
    If you're adamant about doing this in perl, one of the existing html parser modules will prevent re-inventing the wheel in terms of html parsing, so the parsing will be more robust.
    the hardest line to type correctly is: stty erase ^H