in reply to Arrays and Hashes woes

use XML::Rules; my $parser = XML::Rules->new( stripspaces => 3, rules => { 'ID,value' => 'content', field => sub {$_[1]->{ID} => $_[1]->{value}}, page => 'as array', root => sub {$_[1]->{page}}, } ); my $data = $parser->parse(\*DATA); use Data::Dumper; print Dumper($data); $data->[1]{Comments} = "many, of various quality"; # on the other hand the output is rather crazy. We have to convert the + data structure to a format containing all the tags. print $parser->ToXML(root => {page => [map { my $fields = $_; +{field => [map { +{ID => [$_], value => [$fields->{$_}]} } keys % +$fields]} } @$data]}, 0, ' '); # or, if I split it down a bit: print $parser->ToXML(root => {page => [map { hash2fields($_) } @$data] +}, 0, ' '); sub hash2fields { my $fields = shift; return {field => [map { +{ID => [$_], value => [$fields->{$_}]} } +keys %$fields]} } __DATA__ <root> <page> <field> <ID>fname</ID> <value>Jenda</value> </field> <field> <ID>lname</ID> <value>Krynicky</value> </field> </page> <page> <field> <ID>Site</ID> <value>PerlMonks</value> </field> <field> <ID>Nick</ID> <value>Jenda</value> </field> </page> </root>

A little easier to access and modify the data (plus the data structure takes up less memory), the output is a bit scary. Use Data::Dumper to have a look at the structure passed to ->ToXML() to see how it works, it's quite similar to what XML::Simple produced.

P.S.: If anyone has a clever idea how better to specify the datastructure->XML mapping I'm all ears.

Update: I posted a meditation regarding the output at Datastructures to XML.