Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: extract tag content from VLC webserver via XML::Rules

by vagabonding electron (Hermit)
on Jun 25, 2013 at 11:32 UTC ( #1040590=note: print w/ replies, xml ) Need Help??


in reply to extract tag content from VLC webserver via XML::Rules

Keeping your code as is your could just change this part:

my $parser = XML::Rules->new( stripspaces => 7, rules => { info => sub { print "$_[1]->{_content}\n" if $_[1]->{name} eq 'now_playing'; # <- return; }, } ); $parser->parse( $xml);

which prints for me in your example:

John Williams-Losing E.T.-E.T. The Extra-Terrestrial: 20th Anniversary


Comment on Re: extract tag content from VLC webserver via XML::Rules
Select or Download Code
Re^2: extract tag content from VLC webserver via XML::Rules
by rainforest1155 (Initiate) on Jun 25, 2013 at 16:05 UTC

    Sweet, that's exactly what I was looking for!

    Thanks for the regex suggestions as well. I was actually trying the regex route before but it's something I haven't much luck with wrapping my head around so far.

    Love the super fast replies here. You guys are great.

      Okay, I'm stuck again. I can print the now_playing content just fine, but that's not really my intention. I would like to use the data further down the line, but can't seem to get it out of the XML-Rules parser section.

      I tried to populate a string called $nowplaying with it but outside of the parser section, the string prints just empty. Obviously, the print is again just temporary so I can see that it works before I continue.

      How do I get the string contents outside of the parser section? Here's what I have:

      my $nowplaying = ""; my $parser = XML::Rules->new( stripspaces => 7, rules => { info => sub { $nowplaying = $_[1]->{_content} if $_[1]->{name} eq 'now_playing'; return ; } } ); $parser->parse($streaminfo); print $nowplaying;

      I'm sure it's again something real simple I'm missing here.

        Just change the line

        $nowplaying = $_[1]->{_content}

        to

        $nowplaying .= $_[1]->{_content}

        You just missed the contatenation sign. :-)

        Update: Another way could be to populate a data structure with this content, e.g. push @array,$_[1]->{_content} if ... where @array is declared outside the parser.

        Update 2:After some thoughts: in fact your version works for me in the presented example too. However I reckon that you have more than one xml in your real script so that the content is lost if you have <info name="now_playing"/> somewhere later (though you should get a warning "Use of uninitialized value" - both in original and contatenation versions (in your OP you have not "use warnings" on).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1040590]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2014-07-28 23:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (210 votes), past polls