Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

extract tag content from VLC webserver via XML::Rules

by rainforest1155 (Initiate)
on Jun 25, 2013 at 09:32 UTC ( [id://1040568]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

I'm new to Perl and was trying to extract the contents of a specific tag from my VLC webserver xml status page. Here's an example of the XML (I removed a couple of tags in the sections I'm not interested in to keep things shorter:

<root> <fullscreen>0</fullscreen> <audiodelay>0</audiodelay> <apiversion>3</apiversion> <time>1</time> <volume>256</volume> [...] <information> <category name="meta"> <info name="title">CINEMIX</info> <info name="filename">CINEMIX</info> <info name="genre">Soundtracks Classical New Age</info> <info name="now_playing">John Williams-Losing E.T.-E.T. The Extr +a-Terrestrial: 20th Anniversary</info> </category> <category name="Stream 0"> <info name="Bitrate">192 kb/s</info> [...] </category> </information> <stats> <lostabuffers>2</lostabuffers> [...] </stats> </root>

What I'm interested in is just the contents of the <info name="now_playing"> tag which contains artist, title and album.

I already have some code that does return all the <info> tags but I'm not sure how to specifically return just the "now_playing" tag content.

This is the code I managed to hack together so far using parts of code I found in the monastry:

#!/usr/bin/perl use strict; use XML::Rules; use LWP::Simple; my $streaminfo = get("http://127.0.0.1:8080/requests/status.xml"); my $parser = XML::Rules->new( stripspaces => 7, rules => { info => sub { print "$_[1]->{_content}\n"; return; }, } );

I haven't been able to figure out how to filter the results to just the portion I'm interested to. I did look at the XML::Parser docs on cpan.org and have the feeling it's in there, especially the home / office phone example that's touched on in the synopsis, but I can't translate that into any working code.

Thanks for any assistance you can offer.

Sebastian

Replies are listed 'Best First'.
Re: extract tag content from VLC webserver via XML::Rules
by vagabonding electron (Curate) on Jun 25, 2013 at 11:32 UTC

    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

      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.

Re: extract tag content from VLC webserver via XML::Rules
by hdb (Monsignor) on Jun 25, 2013 at 09:42 UTC

    If really all you want is that one item and it is on one line of your XML file, use a regex:

    /^\s*<info name="now_playing">(.*?)<\/info>$/

      If really all you want is that one item and it is on one line of your XML file, use a regex: /^\s*<info name="now_playing">(.*?)<\/info>$/

      If you're going to use a regex don't bother with anchors or or backslashes :)

      my( $fo ) = m{ \Q<info name="now_playing">\E (.*?) \Q</info>\E }sx;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-19 10:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found