in reply to XML::Simple : element vs. attribute in XMLout

You need to change your tree from

my $tree = { arr => { elem => [ { mod => 'mstring1', rp => 'rstring1', subarr => { subelem => [ {content=>'...'}, {content=>'...'}, ] } }, { mod => 'mstring2', rp => 'rstring2', subarr => { subelem => [ {content=>'...'}, {content=>'...'}, ] } }, ], } };
to
my $tree = { arr => { elem => [ { mod => { content => 'mstring1' }, <------ rp => 'rstring1', subarr => { subelem => [ {content=>'...'}, {content=>'...'}, ] } }, { mod => { content => 'mstring2' }, <------ rp => 'rstring2', subarr => { subelem => [ {content=>'...'}, {content=>'...'}, ] } }, ], } };

This can be done programmatically.

You can use ContentKey => ... to specify a name other than "content". I have used _text.

If you're creating the tree using XMLin, use ForceContent => 1. Here's a round-trip example:

use strict; use warnings; use Data::Dumper qw( Dumper ); use XML::Simple qw( ); local $XML::Simple::PREFERRED_PARSER = 'XML::Parser'; my $xs = XML::Simple->new( ForceArray => [qw( elem subelem )], ForceContent => 1, #ContentKey => '_text', KeyAttr => {}, KeepRoot => 1, ); my $tree = $xs->XMLin(<<'__EOI__'); <arr> <elem rp="rstring1"> <mod>mstring1</mod> <subarr> <subelem>...</subelem> <subelem>...</subelem> </subarr> </elem> <elem rp="rstring2"> <mod>mstring2</mod> <subarr> <subelem>...</subelem> <subelem>...</subelem> </subarr> </elem> </arr> __EOI__ { local $Data::Dumper::Indent = 1; print(Dumper($tree)); } print $xs->XMLout($tree);

Update: I had originally phrased the reply as if the OP started from XML. Rearranged contents.
Update: Added instructions on how to transform the tree programmatically.

Replies are listed 'Best First'.
Re^2: XML::Simple : element vs. attribute in XMLout
by grantm (Parson) on May 26, 2010 at 20:53 UTC
    Instead of changing
    mod => 'mstring1',
    to:
    mod => { content => 'mstring1' },
    you could just change it to:
    mod => [ 'mstring1' ],