Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   xml::simple issue with the key (http://www.velocityreviews.com/forums/t915327-xml-simple-issue-with-the-key.html)

hillgoogle@charter.net 12-09-2011 07:59 PM

xml::simple issue with the key
 
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

J. Gleixner 12-09-2011 08:36 PM

Re: xml::simple issue with the key
 
On 12/09/11 19:59, hillgoogle@charter.net 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>



hillgoogle@charter.net 12-09-2011 09:29 PM

Re: xml::simple issue with the key
 
Nice ... that is exactly what I needed. TY !!!

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

Thanks, Mike


All times are GMT. The time now is 04:31 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.