Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

XML::RSS and RSS2 with custom namespaces

by BaldPenguin (Friar)
on Nov 15, 2006 at 20:37 UTC ( #584285=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to generate RSS2 feeds that use custom or non-default namespaces. I have the following restriction, the solution must be Pure Perl, not requiring any compilation before I upload to a hosted server.

I can create the RSS2 feed fine enough with XML::RSS but I need to add some custom data to the feed. I was trying to follow the examples of itunes and yahoo by adding a custom namespace (module) to the feed (ala add_module), but that only seems to work for RSS1 and below.

I am using v1.12, because at v1.20 the module is no longer PP and it's prerequisites require compilation. The code I am using is almost straight out of the docs.

Updated: XML::RSS depends on XML::Parser which is not PurePerl. v1.20 added some date formatting routines that require DateTime to be installed which I cannot load on the remote machine.

Has anyone else had any luck with this?

Everything I've learned in life can be summed up in a small perl script!
  • Comment on XML::RSS and RSS2 with custom namespaces

Replies are listed 'Best First'.
Re: XML::RSS and RSS2 with custom namespaces
by idsfa (Vicar) on Nov 15, 2006 at 21:48 UTC

    So as it says in the docs, you can add namespaces with the add_module method:

    $rss->add_module(prefix=>'my', uri=>'' +); $rss->add_item (title=>$title, link=>$link, my=>{ rating=>$rating }) +;
    Updated: Meh, didn't see the 1.0-only restriction in reading the module code the first time. My bad. Looks like you could just add it in. It's this section:

    # Ad-hoc modules while ( my($url, $prefix) = each %{$self->{modules}} ) { next if $prefix =~ /^(dc|syn|taxo)$/; while ( my($el, $value) = each %{$self->{channel}->{$prefix}} +) { if ( exists( $rdf_resource_fields{ $url } ) and exists( $rdf_resource_fields{ $url }{ $el }) ) { $output .= qq!<$prefix:$el rdf:resource="! . $self->encode($value) . qq!" />\n!; } else { $output .= "<$prefix:$el>". $self->encode($value) ."< +/$prefix:$el>\n"; } } }

    BTW, you do realize that XML::RSS uses XML::Parser, which depends upon expat, which is not pure perl?

    The intelligent reader will judge for himself. Without examining the facts fully and fairly, there is no way of knowing whether vox populi is really vox dei, or merely vox asinorum. — Cyrus H. Gordon
      That snippet is in the as_rss_1_0 subroutine. I guess I could easily hack the module to work correctly, I was wondering if it was something someone else had conquered already and prevent tampering with the module as it sits in CPAN.

      I misspoke earlier, as my requirements get longer to explain: I need modules that are PP or do not require the compilation of modules not already on my hosting platform. They will not add anymore perl modules in the common area so I am missing such essentials as DateTime and XML::LibXML.

      Everything I've learned in life can be summed up in a small perl script!

        Just a thought: Could you tear out the relevant code from XML::RSS and just paste it in your script? Ugly I know, but it sounds like you are in one of those "Yes we know a simple act can facilitate an increase in your productivity, but what we need to know is are you willing to do it against all odds and still meet labor standards?"

      In fact that is what is in my code, however it only works for the RSS1 feeds, as documented. Feeds like itunes and yahoo, require RSS2 feeds. Thus, my problem.

      Everything I've learned in life can be summed up in a small perl script!
Re: XML::RSS and RSS2 with custom namespaces
by pKai (Priest) on Nov 15, 2006 at 21:57 UTC

    Sorry, no helpful advice here. Just an example where a desperate programmer can end up:

    Torn directly from a programm, where I use XML::RSS, but want to include an extension tag and process that in an attached XSL (client-side).

    open (my $F, '>', $file) || die $file; my $feedstr = $rss->as_string(); $feedstr =~ s#(<channel>)(\s*)#$1$2<my:file>$fname</my:fil +e>$2#; die "Improper rss version in generation!?" unless $feedstr =~ s#<(rss version="2\.0")[^>]*>#<$1 xmlns:my="ht +tp://$HOME/">#; $feedstr =~ s#\n#\n<?xml-stylesheet title="XSL_formatting" + type="text/xsl" href="rss.xsl"?>\n#; print $F $feedstr; close $F;
Re: XML::RSS and RSS2 with custom namespaces
by nicholasrperez (Monk) on Nov 16, 2006 at 17:18 UTC
    If you need a pure Perl XML parser implementation you might want to take a look at Ryan Eatmon's work in XML::Stream::Parser. I was using his parser for a while because of that pure Perl requirement that I promptly ignored later because of the issues regarding error handling.

    Last time I used X::S::P it did not handle malformated XML at all. And in fact, it would promptly screw up its internal state into an ever growing memory leak. Plus, it is also very /slow/. Even though he makes use of lower level string manipulation and character by character comparisons, it is still extremely slow compared to other C-based parsers (Expat, aka XML::Parser, etc)

    So. With those caveats, it is an acceptable module to use when you have a pure Perl requirement and you /know/ that the XML you will be parsing doesn't have any funkiness in it.
Re: XML::RSS and RSS2 with custom namespaces (website address update)
by barrycarlyon (Beadle) on Nov 20, 2006 at 02:26 UTC

    Conicidently I was just researching how to do itunes podcasts/rss feeds using perl, so I was pleased to see this node.

    Based on the suggestions in this node, I have modified XML::RSS, I can send you my working copy if you want...

    Currently creating @ a non accessably admin system
    Outputting @

    Update: BTW it is Pure perl so wont need complilation, tho you might need the prerequisies, Carp, XML::Parser, HTML::Entities

    Barry Carlyon
      Indeed, I had to hack something up myself, I was trying not to modify the XML::RSS anymore than I needed to. I wanted to Sub-class it, but the method I wanted to override is not called as a method. It looks like Ask is slowly OOing the module, but hadn't gotten to that method call.

      Some people have submitted patches but no one seems to have submitted patches + tests. I would like to see what you did, and combined with what others have done and my own hacks, maybe we can come up with some patches w/ tests that are acceptable.

      Everything I've learned in life can be summed up in a small perl script!

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://584285]
Approved by grep
Front-paged by andyford
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (1)
As of 2023-09-24 06:02 GMT
Find Nodes?
    Voting Booth?

    No recent polls found