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.


In reply to Re: Arrays and Hashes woes by Jenda
in thread Arrays and Hashes woes by TienLung

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.