xml::simple issue with the key

Discussion in 'Perl Misc' started by hillgoogle@charter.net, Dec 9, 2011.

  1. Guest

    I have a hash defined like:

    my %men = ();
    my $men;
    $men{'men1'} = {'man' => { 'id' => '5',
    'man_name' => ['john doe'],
    'man_nt_name' => ['jdxyz'],
    'man_size' => ['large'] }
    };
    $men{'men2'} = {'man' => { 'id' => '7',
    'man_name' => ['fred Doe'],
    'man_nt_name' => ['fdabc'],
    'man_size' => ['size'] }
    };

    and I am outputting the hash to xml as follows:

    #open the file
    open my $fh, '>:encoding(iso-8859-1)', $prodfile or die
    "open($prodfile): $!";

    # create object
    my $xml = new XML::Simple (KeyAttr=>'man', RootName=>'root');

    #output
    $xml->XMLout(\%men, OutputFile => $fh);

    What I am getting is:
    <root>
    <men1 man="man" id="5">
    <man_name>john doe</man_name>
    <man_nt_name>jdxyz</man_nt_name>
    <man_size>large</man_size>
    </men1>
    <men2 man="man" id="7">
    <man_name>fred Doe</man_name>
    <man_nt_name>fdabc</man_nt_name>
    <man_size>size</man_size>
    </men2>
    </root>

    What I want is:
    <root>
    <man id="5">
    <man_name>john doe</man_name>
    <man_nt_name>jdxyz</man_nt_name>
    <man_size>large</man_size>
    </man>
    <man id="7">
    <man_name>fred Doe</man_name>
    <man_nt_name>fdabc</man_nt_name>
    <man_size>size</man_size>
    </man>
    </root>

    You can see that I don't want the hash key in the xml output.

    Any help is appreciated.

    Mike
    , Dec 9, 2011
    #1
    1. Advertising

  2. J. Gleixner Guest

    On 12/09/11 19:59, wrote:
    > I have a hash defined like:
    >
    > my %men = ();
    > my $men;
    > $men{'men1'} = {'man' => { 'id' => '5',
    > 'man_name' => ['john doe'],
    > 'man_nt_name' => ['jdxyz'],
    > 'man_size' => ['large'] }
    > };
    > $men{'men2'} = {'man' => { 'id' => '7',
    > 'man_name' => ['fred Doe'],
    > 'man_nt_name' => ['fdabc'],
    > 'man_size' => ['size'] }
    > };
    >
    > and I am outputting the hash to xml as follows:
    >
    > #open the file
    > open my $fh, '>:encoding(iso-8859-1)', $prodfile or die
    > "open($prodfile): $!";
    >
    > # create object
    > my $xml = new XML::Simple (KeyAttr=>'man', RootName=>'root');
    >
    > #output
    > $xml->XMLout(\%men, OutputFile => $fh);
    >
    > What I am getting is:
    > <root>
    > <men1 man="man" id="5">
    > <man_name>john doe</man_name>
    > <man_nt_name>jdxyz</man_nt_name>
    > <man_size>large</man_size>
    > </men1>
    > <men2 man="man" id="7">
    > <man_name>fred Doe</man_name>
    > <man_nt_name>fdabc</man_nt_name>
    > <man_size>size</man_size>
    > </men2>
    > </root>
    >
    > What I want is:
    > <root>
    > <man id="5">
    > <man_name>john doe</man_name>
    > <man_nt_name>jdxyz</man_nt_name>
    > <man_size>large</man_size>
    > </man>
    > <man id="7">
    > <man_name>fred Doe</man_name>
    > <man_nt_name>fdabc</man_nt_name>
    > <man_size>size</man_size>
    > </man>
    > </root>
    >
    > You can see that I don't want the hash key in the xml output.


    Then why are you putting it in the data structure?

    XML:Simple converts your data structure, so modify the data structure
    accordingly.

    push( @{ $men{ 'man' } },
    {'id' => '5',
    'man_name' => ['john doe'],
    'man_nt_name' => ['jdxyz'],
    'man_size' => ['large'] } );
    push( @{ $men{ 'man' } },{ 'id' => '7',
    'man_name' => ['fred Doe'],
    'man_nt_name' => ['fdabc'],
    'man_size' => ['size'] }
    );

    [...]
    <root>
    <man id="5">
    <man_name>john doe</man_name>
    <man_nt_name>jdxyz</man_nt_name>
    <man_size>large</man_size>
    </man>
    <man id="7">
    <man_name>fred Doe</man_name>
    <man_nt_name>fdabc</man_nt_name>
    <man_size>size</man_size>
    </man>
    </root>
    J. Gleixner, Dec 9, 2011
    #2
    1. Advertising

  3. Guest

    Nice ... that is exactly what I needed. TY !!!

    So i was creating new structures instead of adding ...

    Thanks, Mike
    , Dec 9, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian Bongiorno

    custom key and hasmap using a ranged key

    Christian Bongiorno, Jun 15, 2004, in forum: Java
    Replies:
    1
    Views:
    456
    Roedy Green
    Jun 15, 2004
  2. Replies:
    3
    Views:
    2,831
  3. Ken
    Replies:
    0
    Views:
    450
  4. M P
    Replies:
    1
    Views:
    465
  5. Erik Wasser
    Replies:
    5
    Views:
    449
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page