Click to See Complete Forum and Search --> : perl and simple xml


toolshed
10-06-2004, 04:14 PM
I have a question on an xml and simple xml.
I want to know why I get the <anon> flag.

#!/usr/bin/perl
# use module
use XML::Simple;
use Data::Dumper;
use strict;
use warnings;
#default category xml
our $CAT_XML_FILE = "categories.xml";

#write data to file
sub writeCatXML($) {
# create object
my @array = shift;

my $xml = new XML::Simple (NoAttr=>1, ForceArray =>1,RootName=>'CATEGORIES');

# convert Perl array ref into XML document $data =
my $data =$ xml->XMLout(@array);

# access XML data
print Dumper($data);

#write data it was been placed in xml form
}

#main test
# create array

my @arr = {'CATEGORY' => [
{
'CAT_ID' => '2',
'CAT_NAME' => 'network',
'CAT_TEXT' => 'network ****'
},
{
'CAT_ID' => '3',
'CAT_NAME' => 'office',
'CAT_TEXT' => 'word ****'
}]};

#writeCatXML(\@arr);
writeCatXML(\@arr);



You see I dont want the <anon> element below. Everything else looks great in the xml, except that <anon>.

It has something to do with the array, but cannot find a reason.


$perl CategoriesWriterXML.plx
$VAR1 = '<CATEGORIES>
<anon>
<CATEGORY>
<CAT_ID>2</CAT_ID>
<CAT_NAME>network</CAT_NAME>
<CAT_TEXT>network ****</CAT_TEXT>
</CATEGORY>
<CATEGORY>
<CAT_ID>3</CAT_ID>
<CAT_NAME>office</CAT_NAME>
<CAT_TEXT>word ****</CAT_TEXT>
</CATEGORY>
</anon>
</CATEGORIES>
';





Just a perl newbie, any help i appreciate.

dchidelf
10-06-2004, 06:47 PM
I think the problem is that you are passing in a reference to an array.
It seems the module expects a reference to a hash.
I think the module is labeling your data anon, because there is no key associated with the data in an array.

I've never used XML::Simple so I'm just guessing, but try the following.

[QUOTE]Originally posted by toolshed

#!/usr/bin/perl
# use module
use XML::Simple;
use Data::Dumper;
use strict;
use warnings;
#default category xml
our $CAT_XML_FILE = "categories.xml";

#write data to file
sub writeCatXML($) {
# create object
my $hash_ref = shift;

my $xml = new XML::Simple (NoAttr=>1, ForceArray =>1,RootName=>'CATEGORIES');

# convert Perl hash ref into XML document $data =
my $data =$ xml->XMLout($hash_ref);

# access XML data
print Dumper($data);

#write data it was been placed in xml form
}

#main test
# create array

my %hash = ('CATEGORY' => [
{
'CAT_ID' => '2',
'CAT_NAME' => 'network',
'CAT_TEXT' => 'network ****'
},
{
'CAT_ID' => '3',
'CAT_NAME' => 'office',
'CAT_TEXT' => 'word ****'
}]);

#writeCatXML(\@arr);
writeCatXML(\%hash);

toolshed
10-07-2004, 09:07 AM
Cool thanks for the help. I would try it but my system crashed. I think that is probably it.