http://qs321.pair.com?node_id=709223

iphony has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have a piece of code that creates an XML as below:
push (@arr, {'Phones' => [ {'Manufacturer' => $unique_manufacturer_model_version[ +$i][9], 'Model' => $unique_manufacturer_model_version[$i][10], 'Version' => $unique_manufacturer_model_version[$i][11], 'Count' => $unique_manufacturer_model_version[$i][12] } +] } ); } my $xml = new XML::Simple(NoAttr=>1,RootName=>'terminalversioncount');
I notice that the output is
<?xml version='1.0'?> <terminalversioncount> <anon> <Phones> <Count>2</Count> <Manufacturer>Sony</Manufacturer> <Model>SE1</Model> <Version>M94</Version> </Phones> </anon>
How can I get rid of the <anon>? I want them to be nested under <Phones> only, for each entry. Another question is, is it possible to append another set of different information (table) to the same XML output file? It seems that RootName can only be set once. Or does it means that I cannot use RootName as a delimiter for different tables? Thanks.

Replies are listed 'Best First'.
Re: XML::Simple - data storage/retrieval
by moritz (Cardinal) on Sep 05, 2008 at 10:18 UTC
    How can I get rid of the <anon>?

    By not storing the hash in an array:

    use strict; use warnings; use XML::Simple; my $d = {'Phones' => [ { 'Manufacturer' => 'somebody', 'Model' => 'something', }]}; my $xml = new XML::Simple(NoAttr=>1,RootName=>'terminalversioncount'); print $xml->XMLout($d); __END__ <terminalversioncount> <Phones> <Manufacturer>somebody</Manufacturer> <Model>something</Model> </Phones> </terminalversioncount>
Re: XML::Simple - data storage/retrieval
by themage (Friar) on Sep 05, 2008 at 10:23 UTC
    Hi iphony, You need to lose that outter array. Like this:
    my $phones= { 'Phones' => [ { 'Manufacturer' => 'Sony', 'Model' => 'SE1', 'Version' => 'M94', 'Count' => 2 }, ], }; my $xml = new XML::Simple(NoAttr=>1,RootName=>'terminalversioncount'); print $xml->XMLout($phones);

Re: XML::Simple - data storage/retrieval
by Cody Pendant (Prior) on Sep 05, 2008 at 12:48 UTC
    It seems that RootName can only be set once.

    This is a rule of XML. An XML file can only have one root-level element.



    Nobody says perl looks like line-noise any more
    kids today don't know what line-noise IS ...
Re: XML::Simple - data storage/retrieval
by Anonymous Monk on Sep 05, 2008 at 10:43 UTC
    Heres how I figured it out, first handcraft xml, then print Dumper, then feed to XMLout
    #!/usr/bin/perl -- use strict; use warnings; use XML::Simple; use Data::Dumper; my $xml = q~<?xml version='1.0'?> <terminalversioncount> <Phones> <Count>2</Count> <Manufacturer>Sony</Manufacturer> <Model>SE1</Model> <Version>M94</Version> </Phones> <Phones> <Count>3</Count> <Manufacturer>Sony3</Manufacturer> <Model>SE13</Model> <Version>M943</Version> </Phones> </terminalversioncount>~; print $xml,$/; print Dumper( XMLin($xml)); my $VAR1 = { 'Phones' => [ { 'Version' => 'M94', 'Count' => '2', 'Manufacturer' => 'Sony', 'Model' => 'SE1' }, { 'Version' => 'M943', 'Count' => '3', 'Manufacturer' => 'Sony3', 'Model' => 'SE13' } ] }; print XMLout($VAR1, NoAttr=>1,RootName=>'terminalversioncount',XMLDecl + => 1); __END__ <?xml version='1.0'?> <terminalversioncount> <Phones> <Count>2</Count> <Manufacturer>Sony</Manufacturer> <Model>SE1</Model> <Version>M94</Version> </Phones> <Phones> <Count>3</Count> <Manufacturer>Sony3</Manufacturer> <Model>SE13</Model> <Version>M943</Version> </Phones> </terminalversioncount> $VAR1 = { 'Phones' => [ { 'Version' => 'M94', 'Count' => '2', 'Manufacturer' => 'Sony', 'Model' => 'SE1' }, { 'Version' => 'M943', 'Count' => '3', 'Manufacturer' => 'Sony3', 'Model' => 'SE13' } ] }; <?xml version='1.0' standalone='yes'?> <terminalversioncount> <Phones> <Count>2</Count> <Manufacturer>Sony</Manufacturer> <Model>SE1</Model> <Version>M94</Version> </Phones> <Phones> <Count>3</Count> <Manufacturer>Sony3</Manufacturer> <Model>SE13</Model> <Version>M943</Version> </Phones> </terminalversioncount>