Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

WURFL web browsers patch and XML::Simple

by jonnyfolk (Vicar)
on Dec 14, 2009 at 08:48 UTC ( #812671=perlquestion: print w/ replies, xml ) Need Help??
jonnyfolk has asked for the wisdom of the Perl Monks concerning the following question:

I have been looking at extracting user_agent information from the wurfl web browsers patch using XML::Simple. I have succeeded in getting the info but it's very explicit - I was hoping that someone might show me how else this could be done. Thanks.

#!/usr/bin/perl -w use strict; print "Content-type: text/html\n\n"; # use module use XML::Simple; use Data::Dumper; use LWP::Simple; my $url = 'http://wurfl.sourceforge.net/web_browsers_patch.xml'; my $content = get($url); # create object my $xml = new XML::Simple; # read XML file my $data = $xml->XMLin($content); # print output my ($key, $value) = %{ $data }; ($key, $value) = %{ $value }; my @keys = keys %{ $value }; foreach (@keys) { print qq~$data->{devices}->{device}->{$_}->{user_agent}<br>\n~; }

Comment on WURFL web browsers patch and XML::Simple
Download Code
Re: WURFL web browsers patch and XML::Simple
by jethro (Monsignor) on Dec 14, 2009 at 10:01 UTC
    foreach my $browser ( values %{$data->{devices}->{device}} ) { push @agents, $browser->{user_agent}; }

    I collected the info into an array, but naturally instead of the push you could just put the print statement as in your example

    UPDATE: After Moritz got through my thick brain on the second try I finally saw the error of my ways and added 'values'

      That's what I was looking for - much more direct!! Thanks very much.

      Update: Unfortunately including it in the code prints a blank! It makes sense to me looking at it - what has gone wrong???

      Update2:The inclusion of values in jethro's update effectively fixes the problem.

      #!/usr/bin/perl -w use strict; print "Content-type: text/html\n\n"; # use module use XML::Simple; use Data::Dumper; use LWP::Simple; my $url = 'http://wurfl.sourceforge.net/web_browsers_patch.xml'; my $content = get($url); # create object my $xml = new XML::Simple; # read XML file my $data = $xml->XMLin($content); my @agents; foreach my $browser ( %{$data->{devices}->{device}} ) { push @agents, $browser->{user_agent}; } print @agents;
      I can't see how that would ever work. for my $browser (%hash) iterates over both keys and values of the hash.

      Since the keys are always strings, I can't see how $browser->{user_agent} works.

      Did I miss something here?

        Ah, ok. So what I need to do to make it work is:

        foreach my $browser ( %{$data->{devices}->{device}} ) { print "$data->{devices}->{device}->{$browser}->{user_agent}<br>"; }

        I think that is an improvement on where I started as it goes into the heart of the data structure rather than disassembling it. However perhaps there is more that can be done?

        Update:The inclusion of values in jethro's update effectively fixes the problem, the solution direct and concise.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2014-08-27 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (253 votes), past polls