atreyu has asked for the wisdom of the Perl Monks concerning the following question:
I am using Perl 5.14.2 and XML::Simple (2.18) to create an XML file from a hash, and I'm having trouble. I can't seem to find the right options to pass to XMLout to create the XML file how I want it. I can modify the way I am creating the hash in the first place, if that is best.
Here's the code:#!/usr/bin/perl use strict; use warnings; use XML::Simple qw(:strict); my $userref = (); $userref->{0}{'hash'}{'name'} = 'joe'; $userref->{0}{'hash'}{'type'} = 'user'; $userref->{1}{'hash'}{'name'} = 'mary'; $userref->{1}{'hash'}{'type'} = 'user'; my $XmlRef = (); # loop thru all keys in userref for my $key(sort keys %$userref){ # the key name my $keyname = 'key'.$key; my $cnt = scalar keys %{$XmlRef->{$keyname}{'devices'}}; my $i=0; for my $foo(keys %{$userref->{$key}{'hash'}}){ $XmlRef->{$keyname}{'devices'}{'dev'.$cnt}{'param'}{$foo}{'content +'} = [ $userref->{$key}{'hash'}{$foo} ]; $i+=1; } } # write XML to file my $tmpfile = '/tmp/testfile.xml'; my $fh; open $fh, '>', $tmpfile or die "open($tmpfile): $!\n"; XMLout($XmlRef, XMLDecl => '<?xml version="1.0" encoding="utf-8"?>', ContentKey => '-content', NoAttr => 0, OutputFile => $fh, KeyAttr => { 'param' => 'id' }, AttrIndent => 1, ); close $fh;
and here's the contents of the generated XML file:
<?xml version="1.0" encoding="utf-8"?> <opt> <key0> <devices> <dev0> <param id="name"> <content>joe</content> </param> <param id="type"> <content>user</content> </param> </dev0> </devices> </key0> <key1> <devices> <dev0> <param id="name"> <content>mary</content> </param> <param id="type"> <content>user</content> </param> </dev0> </devices> </key1> </opt>
however, what i would like is for the "content" key to be removed entirely, e.g.:
<opt> <key0> <devices> <dev0> <param id="name">joe</param> <param id="type">user</param> </dev0> </devices> </key0>
if i simply leave off the "content" key when generating the hash, then I lose the ability to make the "id" an attribute of the "param" key. grrr. can anyone help?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XMLout and keys/attributes
by sauoq (Abbot) on May 09, 2012 at 15:02 UTC | |
by atreyu (Sexton) on May 09, 2012 at 15:29 UTC | |
by sauoq (Abbot) on May 09, 2012 at 15:32 UTC | |
by atreyu (Sexton) on May 09, 2012 at 15:39 UTC | |
by sauoq (Abbot) on May 09, 2012 at 16:14 UTC | |
|
Re: XMLout and keys/attributes
by tobyink (Canon) on May 09, 2012 at 15:02 UTC | |
by atreyu (Sexton) on May 09, 2012 at 15:32 UTC | |
|
Re: XMLout and keys/attributes
by Anonymous Monk on May 09, 2012 at 14:48 UTC | |
by atreyu (Sexton) on May 09, 2012 at 14:56 UTC | |
by atreyu (Sexton) on May 09, 2012 at 15:06 UTC | |
by Anonymous Monk on May 09, 2012 at 15:52 UTC | |
by atreyu (Sexton) on May 09, 2012 at 15:59 UTC |