first, i would change the data structure to:

$tagstack{requirements}->[1] = { text => "A node name", attributes => {contactname => "Jane Smith", contactnumber => "555-1212" } }

note that it is not a hash containing a reference to an array which contains hashrefs. also note the curly braces around the attributes hash instead of square braces.

if XML::Simple is up to the task of parsing your XML, this is fairly straightforward. here's a little script showing how you would go about it:

#!/usr/bin/perl -wT use strict; use XML::Simple; use Data::Dumper; my $data = XML::Simple::XMLin('./test.xml'); # show what we start with print Data::Dumper::Dumper($data); my %tagstack; my @temp; foreach my $h (@{$data->{requirement}}) { my %t; $t{text} = $h->{content}; delete $h->{content}; $t{attributes} = $h; push @temp, \%t; } $tagstack{requirement} = \@temp; # show the finished product print Data::Dumper::Dumper(\%tagstack);

with test.xml being:

<root> <requirement contactname="Joe Average">A power cord.</requirement> <requirement contactname="Jane Smith" contactnumber="555-1212">A node +name</requirement> </root>

it gives the following output:

$VAR1 = { 'requirement' => [ { 'contactname' => 'Joe Average', 'content' => 'A power cord.' }, { 'contactnumber' => '555-1212', 'contactname' => 'Jane Smith', 'content' => 'A node name' } ] }; $VAR1 = { 'requirement' => [ { 'text' => 'A power cord.', 'attributes' => { 'contactname' => 'Joe + Average' } }, { 'text' => 'A node name', 'attributes' => { 'contactnumber' => '5 +55-1212', 'contactname' => 'Jan +e Smith' } } ] };

personally, i think the data structure that XML::Simple produces is more intuitive, but you've probably got a reason for wanting it in the format you do.

anders pearson


In reply to Re: Predefining complex data structures? by thraxil
in thread Predefining complex data structures? by Ionizor

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.