Why don't you create tag handlers for "thread" and "node" that build up your hierachy?
Here something to get you started
#!/usr/bin/perl
use strict;
use warnings;
use XML::Twig;
use Data::Dumper;
my %result;
my $thread= new XML::Twig(
twig_handlers => {
thread => \&thread,
node => \&node,
}
);
$thread->safe_parse(join('',<DATA>));
print Dumper( \%result );
sub thread {
$result{$_->{'att'}->{'id'}}= childrenhash($_->children( 'node' ))
+;
}
sub node {
$_->set_att( structure => { $_->{'att'}->{'id'}, childrenhash($_->
+children( 'node' ))} );
}
sub childrenhash {
my %result;
foreach my $child (@_) {
my $ch= $child->{'att'}->{'structure'};
@result{keys %$ch}= values %$ch;
}
return \%result;
}
__DATA__
<?xml version="1.0" encoding="ISO-8859-1"?>
<thread id="504929">
<node id="504941">
</node>
<node id="504942">
<node id="504950">
<node id="504964">
</node>
</node>
<node id="504953">
</node>
</node>
<node id="504944">
</node>
</thread>
s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|