Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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

In reply to Re: How to simplify the datastructure returned by XML::Twig by Skeeve
in thread How to simplify the datastructure returned by XML::Twig by davido

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2023-01-29 22:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?