Parsing Through Perl XML Data

Posted on 2012-09-13
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 = "";
  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);
        $dataresponse = $response->content(); #Loaded data from FINAL_XML
    return $dataresponse;

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


Open in new window

    <season xmlns="" season="2012" season_type="REG">
<team id="BUF" name="Bills" market="Buffalo">
        <player id="100" name="Nick Barnett" games_played="1" games_started="1" jersey="50" position="OLB">
            <defense tackle="7" ast="2" />
        <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 id="102" name="Fred Jackson" games_played="1" games_started="1" jersey="22" position="RB">
            <rushing att="6" yds="15" td="0"/>

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?
Question by:Thread7
    LVL 25

    Accepted Solution

    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}}){
    LVL 1

    Author Comment

    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}}){

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    This is an explanation of a simple data model to help parse a JSON feed
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to dynamically set the form action using jQuery.

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now