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: Basic XML Loop : Edit Log



ogit2
Novice

Aug 24, 2017, 9:33 AM


Views: 2923
Basic XML Loop

Firstly, we are trying to loop around an XML structure and just need some help on the correct syntax.

Secondly we need to know how to reference an XML structure as <AddInfo Code="E" ActionCode="1Z"/> i.e. How do we print the data. AddInfo contains no data when we use


Code
print "Additional Info: $data->{Consignment}[0]->{Addinfo} \n";




A sample of the data is below


Quote
<?xml version="1.0" encoding="UTF-8"?>

<ConsignmentList xmlns="http://www.w3.org/2001/XMLSchema">

<Consignment>
<Header>
<ManifestDate>2017-08-23</ManifestDate>
</Header>
<Date>2017-08-23</Date>
<Name>SYNTHITE LIMITED</Name>
<AddInfo Code="E" ActionCode="1Z"/>
</Consignment>

<Consignment>
<Header>
<ManifestDate>2017-08-23</ManifestDate>
</Header>
<Date>2017-08-23</Date>
<Name>SYNTHITE LIMITED</Name>
<AddInfo Code="E" ActionCode="1Z"/>
</Consignment>

</ConsignmentList>





Code
#!C:\Perl\bin\perl.exe 

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";

# Loop through each Consignment XML reference
# This does not work and comes back with error on the
# foreach line of 'Not a HASH reference at test.pl line xx'
foreach my $entry ( keys %{$data->{'Consignment'}} )
{
print "Here\n";
print $data->{$entry}->{Header}->{ManifestDate} . "\n";
}



(This post was edited by ogit2 on Aug 24, 2017, 10:09 AM)


Edit Log:
Post edited by ogit2 (Novice) on Aug 24, 2017, 9:35 AM
Post edited by ogit2 (Novice) on Aug 24, 2017, 10:09 AM


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

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