Hello,

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?


In reply to XMLout and keys/attributes by atreyu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.