?
Solved

Parsing Through Perl XML Data

Posted on 2012-09-13
2
Medium Priority
?
735 Views
Last Modified: 2012-09-13
I'm just learning PERL. I've read some docs on parsing XML and reused some of my colleague's code. But I can't seem to get it to simply print out a list of the 3 football player names in this data. Please help. Here is the PERL code:

   #! /usr/bin/perl -w
use strict;
use DBI;
use XML::Simple;
use LWP::UserAgent;
use HTTP::Request;
use Time::gmtime;
use Time::Local;
use Data::Dumper;

sub statsInit(){
  my $xml_api_url = "http://myprovider.com/nfldata.xml";
  my $xml = getXML($xml_api_url);
  my $xmlData = xml2Simple($xml);
  print "Hello World";
  foreach my $player (@{$xmlData->{player}}){
    my $playername = $player->{name};
    print "pn = $playername | ";
  }
}


sub getXML($){
    my ($xmlURL) = @_;
    my $dataresponse = '';
    my $agent = LWP::UserAgent->new(env_proxy => 1, keep_alive => 1, timeout => 100);
    my $header = HTTP::Request->new(GET => $xmlURL);
    my $request = HTTP::Request->new('GET', $xmlURL, $header);
    my $response = $agent->request($request);
    if($response->is_success){
        $dataresponse = $response->content(); #Loaded data from FINAL_XML
    }
    return $dataresponse;
}

sub xml2Simple(){
    my ($xml) = @_;
    my $xmlObj = XML::Simple->new(KeyAttr => {});
    my $xmlData = '';
    my $parsedData = '';
    if($xml){
        $xmlData = $xmlObj->XMLin($xml);
    }
    return $xmlData;
}

statsInit();

Open in new window


-------- NOW BELOW IS THE RETRIEVED XML DATA --------
    <season xmlns="http://myprovider.com/schema/nfl/statistics-v1.0.xsd" season="2012" season_type="REG">
<team id="BUF" name="Bills" market="Buffalo">
    <players>
        <player id="100" name="Nick Barnett" games_played="1" games_started="1" jersey="50" position="OLB">
            <defense tackle="7" ast="2" />
        </player>
        <player id="101" name="Ryan Fitzpatrick" games_played="1" games_started="1" jersey="14" position="QB">
            <rushing att="2" yds="8" />
            <passing att="32" cmp="18" yds="195" td="3" int="3"/>
        </player>
        <player id="102" name="Fred Jackson" games_played="1" games_started="1" jersey="22" position="RB">
            <rushing att="6" yds="15" td="0"/>
        </player>
    </players>
</team>
</season>

Open in new window


So my code seems to retrieve the data ok, and I get no errors. "Hello World" gets printed. But it never goes into the players loop and doesn't print any player names. What am I doing wrong?
0
Comment
Question by:Thread7
2 Comments
 
LVL 25

Accepted Solution

by:
lwadwell earned 2000 total points
ID: 38397731
The structure of the XML results in a more layered hierarchy.  
If you do
    print Dumper(\$xmldata);
you will see the layers.
For that xml file ... try
   foreach my $player (@{$xmlData->{team}->{players}->{player}}){
0
 
LVL 1

Author Comment

by:Thread7
ID: 38397743
I figured it out. I guess I had to change this line.

foreach my $player (@{$xmlData->{player}}){

to this:

foreach my $player (@{$xmlData->{team}{players}{player}}){
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Simple Linear Regression
Progress
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question