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: Beginner: Re: [ogit2] Basic XML Loop : Edit Log



BillKSmith
Veteran

Aug 24, 2017, 1:59 PM


Views: 2675
Re: [ogit2] Basic XML Loop


Code
#!C:\Perl\bin\perl.exe  
use strict;
use warnings;

use POSIX;
use XML::Simple;
use Data::Dumper;

my $file = "xmlfile.xml";
my $xml = new XML::Simple;
my $data = $xml->XMLin("$file");

print Dumper($data);

# This print below works ok
print "MANIFEST DATE: $data->{Consignment}[0]->{Header}->{ManifestDate} \n";

#foreach my $entry ( keys %{$data->{'Consignment'}} )
foreach my $entry (@{$data->{'Consignment'}} )
{
print "Here\n";
#print $data->{$entry}->{Header}->{ManifestDate} . "\n";
print $entry->{Header}->{ManifestDate} . "\n";
}

OUTPUT:
$VAR1 = {
'Consignment' => [
{
'Date' => '2017-08-23',
'Name' => 'SYNTHITE LIMITED',
'AddInfo' => {
'Code' => 'E',
'ActionCode' => '1Z'
},
'Header' => {
'ManifestDate' => '2017-08-23'
}
},
{
'Header' => {
'ManifestDate' => '2017-08-23'
},
'Name' => 'SYNTHITE LIMITED',
'Date' => '2017-08-23',
'AddInfo' => {
'ActionCode' => '1Z',
'Code' => 'E'
}
}
],
'xmlns' => 'http://www.w3.org/2001/XMLSchema'
};
MANIFEST DATE: 2017-08-23
Here
2017-08-23
Here
2017-08-23


UPDATE: Add explanation:

Your problem has nothing to do with XML. The module XML::SIMPLE has already converted it to a perl complex data structure. Refer Data Structures Cookbook. This tutorial assumes that you understand all the documents in its "SEE ALSO" section. (Especially the "Using References" section of perlref)

In your example, "$data->{'Consignment'}" is an array reference, not a hash reference. Inside the loop, you must dereference $entry, not $data.
Good Luck,
Bill

(This post was edited by BillKSmith on Aug 24, 2017, 6:44 PM)


Edit Log:
Post edited by BillKSmith (Veteran) on Aug 24, 2017, 6:44 PM


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

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