Dave Adams wrote:

On 3/26/07, Rob Dixon <[EMAIL PROTECTED]> wrote:

Dave Adams wrote:
>
> What are the general steps to building a hash from and xml document?
>
> Here are my steps:
>
> 1. Read in xml document using XML::Simple
> 2. Create and empty hash
> 3. Loop through $VAR1 (the anonymous datastructure) and populate hash
>
> Is this the general idea or is there a simplier way?

Hi Dave

The result of XMLin is already a reference to a hash representation of
the XML. If you're looking at the XML::Simple documentation that says
you can dump the hash using Data::Dumper then that's not a part of
using XML::Simple - it's just a way of examining the hash data so that
you can see what you've got.

#<?xml version="1.0" encoding="ISO-8859-1" ?>
#<DATA>
#<RECORD>
# <COLLNAME>FBIS2004</COLLNAME>
# <DOCI>CP1</DOCI>
#</RECORD>
#<RECORD>
# <COLLNAME>FBIS2005</COLLNAME>
# <DOCI>CP2</DOCI>
#</RECORD>
#</DATA>


#!/usr/bin/perl
use XML::Simple;
use Data::Dumper;

# create object
$xml = new XML::Simple (KeyAttr=>[]);

# read XML file
$data = $xml->XMLin("datafile.xml");

# dereference hash ref
# access <RECORD> array

foreach $e (@{$data->{RECORD}})
{
print "doci: ", $e->{DOCI}, "\n";
print "collname: ", $e->{COLLNAME}, "\n";
print "\n";
}

# Puts XML file into hash
my %starrecords=();
foreach $e (@{$data->{RECORD}})
{
$starrecords {$e->{DOCI}} = $e->{COLLNAME} ;
}

# Prints the hash
print "Hash Output: \n";
foreach my $key_value_pairs (%starrecords) {
print "$key_value_pairs";
}
print "\n";

# Prints the keys of the hash
print "Hash Keys Output: \n";
foreach my $key_values (keys %starrecords) {
print "$key_values\n";
}

# Prints the values of the hash
print "Hash Values Output: \n";
foreach my $value_values (values %starrecords) {
print "$value_values\n";
}

Sorry, do you have a question?

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to