Question about printing XML using XML::Simple

Question about printing XML using XML::Simple

am 15.11.2007 04:15:07 von some

I'm using XML::Simple to read an XML file named test.xml, which is listed
below. I'm trying to print individual elements in the xml file like this:


my $xml = new XML::Simple(KeyAttr=>[], ForceArray => 1);
my $ref = $xml->XMLin($xmlfile);
##print Dumper($ref);

#These don't work

print $ref->{USER}->{NAME}->{FIRST_NAME};
print $ref->{XMLFILE}->{USER}->{NAME}->{FIRST_NAME};
print $ref->{USER}->{NAME}->{FIRST_NAME}->[0];


How can I print an individual element like ?

-Thanks


test.xml:




FRED
FLINTSTONE


123 Main Street
Bedrock
CA
23912


Re: Question about printing XML using XML::Simple

am 15.11.2007 05:57:09 von stratfan

This should work. You were on the right track for your syntax for the
references in the converted hash. The problem might have been with
the call to XMLin.


stratfan

=================================

#!/usr/bin/perl

use XML::Simple;


my $testxml = <<"EOF";



FRED
FLINTSTONE


123 Main Street
Bedrock
CA
23912



EOF


my $xmlResultHash = XMLin($testxml);

#XMLin defaults to dropping the top XML layer (XMLFILE in this case)

my $firstname = $xmlResultHash->{USER}->{NAME}->{FIRST_NAME};
my $lastname = $xmlResultHash->{USER}->{NAME}->{LAST_NAME};
my $city = $xmlResultHash->{USER}->{ADDRESS}->{CITY};

print "firstname = $firstname\n";
print "lastname = $lastname\n";
print "city = $city\n";

exit;

======================

myserver:/home/stratfan # perl xmltest.pl
firstname = FRED
lastname = FLINTSTONE
city = Bedrock
myserver:/home/stratfan #

Re: Question about printing XML using XML::Simple

am 15.11.2007 06:28:38 von Iain Chalmers

In article ,
somebody wrote:

> I'm using XML::Simple to read an XML file named test.xml, which is listed
> below. I'm trying to print individual elements in the xml file like this:
>
>
> my $xml = new XML::Simple(KeyAttr=>[], ForceArray => 1);
> my $ref = $xml->XMLin($xmlfile);
> ##print Dumper($ref);
>
> #These don't work
>
> print $ref->{USER}->{NAME}->{FIRST_NAME};
> print $ref->{XMLFILE}->{USER}->{NAME}->{FIRST_NAME};
> print $ref->{USER}->{NAME}->{FIRST_NAME}->[0];
>
>
> How can I print an individual element like ?

What did that commented out print Dumper($ref) show you?

did you try:

print $ref->{USER}->[0]->{NAME}->[0]->{FIRST_NAME}->[0];

I suspect that KeyAttr=>[] and ForceArray => 1 might not be what you
want...

cheers,

big

--
"Everything you love, everything meaningful with depth and history,
all passionate authentic experiences will be appropriated, mishandled,
watered down, cheapened, repackaged, marketed and sold to the people
you hate." Mr Jalopy quoting Hooptyrides (on jalopyjunktown.com)