CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: Looping using XMLSimple: Edit Log



satnamx
New User

Jul 28, 2012, 11:33 AM


Views: 3526
Looping using XMLSimple

Hi All,

I have the below sample XML dump structure after reading it into XMLSimple:


Ive also uploaded an attachment of the actual XML example as the below is formatting screwed up!


$VAR1 = {
'Response' => [
{
'RTAG' => 'closure47',
'UUID' => 'Group000',
'Group' => [ {
'NAME' => 'null',
'GroupRef' => [
{
'GID' => 'Canada'
},
{
'GID' => 'United States'
}
],
'GID' => 'Group000',
'TYPE' => 'SELLFIRM',
'Property' => [
{
'VALUE' => 'SELLFIRM'
'NAME' => 'Level'
},
{
'VALUE' => 'Portfolio Type 1',
'NAME' => 'Portfolio Type'
},
............

............

...........



The below codes loops through above and returns the correct 'GID' within the 'Group' nodes:

my $i = 0;
my $j = 0;
foreach $e (@{ $data->{Response} }) {
for ($i = 0; $i < scalar (@{$e->{Group}}); $i++) { #Loop through the 'Group'
print "{Group}[$i]->{GID}: ", $e->{Group}[$i]->{GID}, "\n"; # get GID's

}
}

Im trying to use the above to output the 'GID's within the 'Groupref' nodes, something along the lines of this:

my $i = 0;
my $j = 0;
foreach $e (@{ $data->{Response} }) {
for ($i = 0; $i < scalar (@{$e->{Group}}); $i++) { #Loop through the 'Group'
print "{Group}[$i]->{GID}: ", $e->{Group}[$i]->{GID}, "\n"; # get GID's

for ($j = 0; $j < scalar (@{$e->{Group}->{GroupRef}}); $j++) { #Loop through the 'Group'
print "{Group}[$i]->{GroupRef}[$j]->{GID}: ", $e->{Group}[$i]->{GroupRef}[$j]->{GID}, "\n"; #'Canada'
}
}
}



but I get the below error:

{Group}[0]->{GID}: Group000
Can't use an undefined value as an ARRAY reference at testXMLSimple.pl line 51.

Clearly the portion of the for loop scalar (@{$e->{Group}->{GroupRef}}); ie trying to find the size of the array is causing the issue.

If i replaced the for loop with something like for ($j = 0; $j < 10; $j++) ... ths would work but obviously as a test, I cant harcode the upper limit to 10!

Ive also uploaded an attachment if the formatting of the XML is screwed up!

any ideas greatly appreciated?

Kind regards

Satnam



Any ideas on how to find the size of the array in the line


(This post was edited by satnamx on Jul 29, 2012, 3:47 AM)
Attachments: XMLSimple_Issue_ForLooping.txt (3.12 KB)


Edit Log:
Post edited by satnamx (New User) on Jul 29, 2012, 3:47 AM: Make it know attachment is avaialable


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives