/ Published in: Perl
Read in a remote XML file, change the pubDate date format, and output to specific local file.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
# Modify XML Feed Items' pubDate to different date format # Author: Karl Horky # Date: 29 September 2009 # # Sample Input: # <?xml version="1.0" encoding="UTF-8"?> # <rss version="2.0"> # <channel> # <title>News</title> # <link>http://www.news.com/</link> # <description>The latest headlines</description> # <language>en-us</language> # <copyright>Copyright �© 2009</copyright> # <ttl>5</ttl> # <item> # <title>News Item 1</title> # <description>Item Description 1</description> # <link>http://www.news.com/news_release_1.htm</link> # <pubDate>Tue, 29 Sep 2009 17:47:42 GMT</pubDate> # </item> # # <item> # <title>News Item 2</title> # <description>Item Description 2</description> # <link>http://www.news.com/news_release_2.htm</link> # <pubDate>Mon, 24 Aug 2009 07:00:00 GMT</pubDate> # </item> # </channel> # </rss> # # # # Sample Output file: # <rss version="2.0"> # <channel> # <title>News</title> # <link>http://www.news.com/</link> # <description>The latest headlines</description> # <language>en-us</language> # <copyright>Copyright �© 2009</copyright> # <ttl>5</ttl> # <item> # <title>News Item 1</title> # <description>Item Description 1</description> # <link>http://www.news.com/news_release_1.htm</link> # <pubDate>September 29, 2009</pubDate> # </item> # # <item> # <title>News Item 2</title> # <description>Item Description 2</description> # <link>http://www.news.com/news_release_2.htm</link> # <pubDate>August 24, 2009</pubDate> # </item> # </channel> # </rss> # #!/usr/local/bin/perl use Data::Dumper; use XML::Simple; use Date::Manip; $output = 'feed.xml'; # The location of your output file my $ua = LWP::UserAgent->new; $ua->timeout(10); $ua->env_proxy; my $response = $ua->get('http://www.example.com/feed.xml'); # The location of the input file if ($response->is_success) { $xml = $response->content; } else { } my $xs = new XML::Simple(keeproot => 1,searchpath => ".", forcearray => 1, keyattr => [key, tag]); my $ref = $xs->XMLin($xml, KeepRoot => 1); foreach my $item (@{$ref->{rss}->[0]->{channel}->[0]->{item}}){ my $currDate = \$item->{pubDate}->[0]; $$currDate = UnixDate($$currDate,"%B %d, %Y"); } my $xml = $xs->XMLout($ref, KeepRoot=>1);