use strict ;
use warnings ;
use XML::Simple; #ppm install XML::Simple
use Data::Dumper; #ppm install Data::Dumper - used just for debugging
sub main
{
print "START SCRIPT " ;
my $fileToParse = 'C:/Temp/CDIP/scripts/perl/nps_all_workflows.xml' ;
# create object
my $objXml= new XML::Simple;
# read XML file
my $data = $objXml->XMLin("$fileToParse");
# print "\n FirstLevel is " . $objXml->{'POWERMART'} ;
printHashKeyValues ($data ) ;
} #eof main
#for each
sub printHashKeyValues
{
my $refHash = shift ;
my $parentKey = shift ;
my $parentValue = shift ;
while( my ($key, $value) = each %$refHash)
{
if ( defined ( $key ) )
{
if ( ref ($refHash->{"$key"}) eq 'HASH' )
{
my $newRefHash = $refHash->{"$key"} ;
print " \n The key is a hash " ;
printHashKeyValues ($newRefHash , $key , $value) ;
}
if ( ref ($refHash->{"$key"}) eq 'ARRAY' )
{
print " \n the key is an ARRAY " ;
printArrayValues ( $refHash->{"$key"} ) ;
}
} #eof if ( defined ( $key ))
if ( defined ( $value) )
{
if ( ref ($refHash->{"$value"}) eq 'HASH' )
{
my $newRefHash = $refHash->{"$value"} ;
print " \n The value is a hash " ;
printHashKeyValues ($newRefHash , $key , $value) ;
}
if ( ref ($refHash->{"$value"}) eq 'ARRAY' )
{
print " \n the value is an ARRAY " ;
printArrayValues ( $refHash->{"$value"} ) ;
}
} #eof if defined ( $value )
print "\n key: $key, value: $value.\n";
} #eof while
} #eof sub
#Foreach element of the array if it is array call itself recursively
# if the element is a hash ref print the hash recursively
sub printArrayValues
{
my $arrRef = shift ;
my @array = @$arrRef;
my $parrentArrayElement = shift ;
print "printArrayValues CALLED " ;
foreach my $arrayElement ( @array )
{
if (defined ( $arrayElement ) )
{
if ( ref ($arrayElement) eq 'HASH' )
{
print " \n The \$arrayElement is a hash FROM THE ARRAY " ;
printHashKeyValues ($arrayElement ) ;
} #eof if
if ( ref ($arrayElement) eq 'ARRAY' )
{
print " \n The \$arrayElement is a ARRAY FROM THE ARRAY " ;
printArrayValues ($arrayElement ) ;
} #eof if
print "\n \$arrayElement is $arrayElement " ;
} #eof if ( defined ( $arrayElement ) )
} #eof foreach
} #eof sub
# print output
#print Dumper($data);
#action !!
main();
1 ;
#Purpose:
#open an xml file and parse it with XML::Simple
#since the XML::Simple module represents the xml document as a data structure collection of hash of hashes and array
#go trough the initial hash and if the key or the value are hashes print hash recursively
# if the key or the value are array refs print hte array refs recursively
#Version 1.0
#TODO: add recognition for parent keys and values
No comments:
Post a Comment
- the first minus - Comments have to be moderated because of the spammers
- the second minus - I am very lazy at moderating comments ... hardly find time ...
- the third minus - Short links are no good for security ...
- The REAL PLUS : Any critic and positive feedback is better than none, so your comments will be published sooner or later !!!!