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

Greetings!

I have a hash ref:

$VAR1 = { 'Last' => 'Smith', 'First' => 'John', };
When I run it though XMLout like this:
print XMLout($hashref);
I get this:
<opt Last="Smith" First="John" />
What I want is this:
<opt> <Last>Smith</Last> <First>First</First> </opt>
I have looked though the docs and have tried quite a few option combos, but I am not getting quite what I want. What options do I need to use? Thanks for the help!

Replies are listed 'Best First'.
Re: XMLSimple XMLout, formatting output
by ikegami (Patriarch) on Apr 01, 2009 at 19:57 UTC
    ForceContent => 1, I think. Of course, the result of XMLin will be different than what you presented.

    Update: Yup.

    use strict; use warnings; use XML::Simple qw( ); my $xml = <<'__EOI__'; <doc> <Last>Smith</Last> <First>First</First> </doc> __EOI__ my $xs = XML::Simple->new( ForceContent => 1, ); print( $xs->XMLout( $xs->XMLin($xml) ) );
    <opt> <First>First</First> <Last>Smith</Last> </opt>
      Well, ForceContent is an XMLin option. I am not using XMLin at all, just XMLout.
        Ah, then don't specify it. You'll still need to change the layout of your data. Good thing you have Perl handy.
Re: XMLSimple XMLout, formatting output
by mr_mischief (Monsignor) on Apr 01, 2009 at 20:52 UTC
    You may want NoAttr for your XMLOut. Then again, you may want to forego XML::Simple for one of the more recommended XML modules like XML::Twig, XML::LibXML, or XML::Rules.

    There are also others that look promising at a glance, for that matter, but I've not heard much about them. Any recommendations for or against XML::TreePP, XML::Smart, XML::DOM2 or their comparisons to the ones already mentioned I'd be happy to find somewhere.

Re: XMLSimple XMLout, formatting output
by grantm (Parson) on Apr 02, 2009 at 01:02 UTC

    XMLout() will render simple scalar values as attributes unless the NoAttr option is in effect. So one approach would be: if you don't want a value to be an attribute, don't represent it as a simple scalar, e.g.: change your source data to:

    $VAR1 = { 'Last' => [ 'Smith' ], 'First' => [ 'John' ], };