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 ( #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? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2023-02-05 11:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (31 votes). Check out past polls.

    Notices?