Maybe you should step away and rethink your algorithm. Maybe it would be better to create the tree structure of sections first and then export that as XML. As your read the sections from the CSV you can keep a hash (or array if the nodeIds are positive integers and are not too sparse) of references to the individual sections and add the children easily. Without having to search for them using XPath or any other advanced thing:

my @columns = qw(parentNodeId nodeId level canode label); my $CSV = <<'*END*'; 0,1,0,blah,bloh 1,2,1,foo,faaa 1,3,1,bar,barrar 2,4,2,baz,bazbaz 2,5,2,aaa,aaaaa 3,6,2,bbb,bbbbbbab 4,7,3,zzz,zzzzz *END* my (%sections, $root); my @navmod = split /\n/, $CSV; foreach my $line (@navmod) { my %current; @current{@columns} = split /,/, $line; delete $current{level}; my $nodeId = $current{nodeId}; $sections{$nodeId} = \%current; my $parentNodeId = delete $current{parentNodeId}; if ($parentNodeId) { push @{$sections{$parentNodeId}{section}}, \%current; } else { $root = \%current; } } use XML::Rules; my $parser = XML::Rules->new( rules => []); print $parser->ToXML(section => $root, 0, ' ');
And if your CSV is already sorted enough so that no node is referenced sooner than it's defined you do not have to read the whole CSV into an array and sort it more, but may read it line by line and build the tree.

It's actually fairly easy to tweak the code to work even if sections do get referenced before they are defined:

my @columns = qw(parentNodeId nodeId level canode label); my (%sections, $root); while (my $line = <DATA>) { chomp($line); my %current; @current{@columns} = split /,/, $line; delete $current{level}; my $nodeId = $current{nodeId}; my $parentNodeId = delete $current{parentNodeId}; if ($sections{$nodeId}) { %{$sections{$nodeId}} = ( %current, %{$sections{$nodeId}}); } else { $sections{$nodeId} = \%current; } if ($parentNodeId) { push @{$sections{$parentNodeId}{section}}, $sections{$nodeId}; } else { $root = $sections{$nodeId}; } } use XML::Rules; my $parser = XML::Rules->new( rules => []); print $parser->ToXML(section => $root, 0, ' '); __DATA__ 3,6,2,bbb,bbbbbbab 1,2,1,foo,faaa 2,4,2,baz,bazbaz 2,5,2,aaa,aaaaa 1,3,1,bar,barrar 4,7,3,zzz,zzzzz 0,1,0,blah,bloh


In reply to Re: Need help with XML::Twig, something strange is happening!! by Jenda
in thread Need help with XML::Twig, something strange is happening!! by stevee

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.