perl and simple xml


Results 1 to 3 of 3

Thread: perl and simple xml

  1. #1
    Join Date
    Feb 2001
    Posts
    241

    perl and simple xml

    I have a question on an xml and simple xml.
    I want to know why I get the <anon> flag.
    Code:
    #!/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.

    Code:
    $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.
    later,
    toolshed#7

    deserving got nothing to do with it.......

  2. #2
    Join Date
    Jan 2001
    Location
    Somewhere in middle America
    Posts
    164

    Re: perl and simple xml

    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
    Code:
    #!/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);
    My Machine:
    Maytag SAV5905
    710 rpm Stainless Steel Drum
    Dual boot: Gentoo / Tide

  3. #3
    Join Date
    Feb 2001
    Posts
    241
    Cool thanks for the help. I would try it but my system crashed. I think that is probably it.
    later,
    toolshed#7

    deserving got nothing to do with it.......

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •