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

 



ogit2
Novice

Aug 24, 2017, 9:33 AM

Post #1 of 4 (1206 views)
Basic XML Loop Can't Post

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)


BillKSmith
Veteran

Aug 24, 2017, 1:59 PM

Post #2 of 4 (1193 views)
Re: [ogit2] Basic XML Loop [In reply to] Can't Post


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)


ogit2
Novice

Aug 25, 2017, 2:01 AM

Post #3 of 4 (1176 views)
Re: [ogit2] Basic XML Loop [In reply to] Can't Post

Hi

After your explanation, this now makes a lot more sense. Very impressive on how Perl does the conversion so well. I have a lot of reading up to do :).

Thank you


Quote
Your problem has nothing to do with XML. The module XML::SIMPLE has already converted it to a perl complex data structure.



Laurent_R
Veteran / Moderator

Aug 26, 2017, 10:59 AM

Post #4 of 4 (1156 views)
Re: [ogit2] Basic XML Loop [In reply to] Can't Post

Please note that the use of the XML::Simple module is deprecated for new projects.

From its own documentation:

Quote
You really don't want to use this module in new code.
(...)
The use of this module in new code is discouraged. Other modules are available which provide more straightforward and consistent interfaces. In particular, XML::LibXML is highly recommended and XML::Twig is an excellent alternative.


See: http://search.cpan.org/~shlomif/XML-LibXML-2.0129/LibXML.pod and http://search.cpan.org/perldoc?XML%3A%3ATwig for those alternatives.

 
 


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

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