Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

XML Parser question

by idcrisis (Initiate)
on Sep 20, 2012 at 08:57 UTC ( #994612=perlquestion: print w/replies, xml ) Need Help??
idcrisis has asked for the wisdom of the Perl Monks concerning the following question:

Hello ,

I am trying to parse a valid xml document using XML::RAI and see that it can identify limited number of fields that i can retrieve. How can i retrieve author or description or replycount fields from the XML document given below.

#!/usr/bin/perl use XML::RAI; my $xml=`curl -ksS ""`; $xml =~ s/\15//g; my $feed = XML::RAI->parse($xml); for ( reverse (@{$feed->items}) ){ my $title=$_->title; my $date =$_->created ; my $date_modified; my $url =$_->link ; }

XML Format:

<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:jf=""> <channel> <item> <title>xxxxxxxxxxxxxxxxx</title> <link></link> <description>yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy</description> <pubDate>Wed, 19 Sep 2012 20:57:16</pubDate> <jf:creationDate>Wed, 19 Sep 2012 20:57:16 </jf:creationDate> <jf:modificationDate>Wed, 19Sep2012 20:57:16 </jf:modification +Date> <jf:date>Sep 19, 2012</jf:date> <jf:author>author1</jf:author> <jf:replyCount>3</jf:replyCount> </item> </channel> </rss>

Replies are listed 'Best First'.
Re: XML Parser question
by tobyink (Abbot) on Sep 20, 2012 at 11:22 UTC

    It's a hack, but it looks like you should be able to "teach" XML::RAI::Item about new fields like this:

    $XML::RAI::Item::XMap->{my_author} = [qw( jf:author )];

    ... and then just use $_->my_author.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: XML Parser question
by shmem (Chancellor) on Sep 20, 2012 at 15:17 UTC
    How can i retrieve author or description or replycount fields from the XML document given below.

    Strip the bogus namepsace identifier jf. Where did you get that from? certainly not from

    $xml =~ s/jf://g;

    Add the attributes you want according to the docs:

    XML::RAI::Item->add_mapping($_,$_) for qw(modificationDate author replyCount);

    There might be a way to register xmlns:jf within the XML modules suite you are using. Reading the docs might help...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://994612]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2018-03-18 02:21 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (228 votes). Check out past polls.