Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: How to simplify the datastructure returned by XML::Twig

by Skeeve (Parson)
on Nov 06, 2005 at 09:09 UTC ( [id://506089]=note: print w/replies, xml ) Need Help??


in reply to How to simplify the datastructure returned by XML::Twig

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://506089]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-23 13:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found