Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I've been puzzling over this too long, and am sure I'm just not looking at it the right way. Hopefully someone with some experience with XML::Twig will have a suggestion.

I'm working on improving some of the datastructures returned by PerlMonks::Mechanized. One of the easiest ones to work with is just eluding me. Specifically, I'm updating the method that parses the Monastery's XML thread ticker returning a concise but useful datastructure.

The problem is that the datastructure returned by XML::Twig (and previously XML::Simple) isn't as simple as it should be; it contains extra levels of indirection that are unneeded.

It wouldn't be all that hard to simply traverse the structure returned by XML::Twig, modifying the datastructure to remove extra indirection, but somehow I believe XML::Twig is capable of giving me what I want in the first place. But over the past few days I've convinced myself that I don't understand XML::Twig enough to get the most out of this highly flexible module.

Here is a simplified snippet of code that gives an example of what I'm doing.

use strict; use warnings; use XML::Twig; use Data::Dumper; my $xml; { local $/ = ''; $xml = <DATA>; } print $xml, "\n"; my $twig = XML::Twig->new(); $twig->safe_parse( $xml ); my $struct = $twig->simplify( forcearray => 1, keyattr => [ qw/id/ ], # group_tags => { 'node id' => 'id' }, ); print Dumper $struct; __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>

As you can see, I've commented out the group_tags attribute, because it wasn't gaining me anything, at least how I was using it.

The output I'm getting is:

$VAR1 = { 'node' => { '504942' => { 'node' => { '504953' => undef, '504950' => { 'node' => { '504964' => undef } } } }, '504941' => undef, '504944' => undef } };

I'm close, but what I really want is:

$VAR1 = { '504942' => { '504953' => undef, '504950' => { '504964' => undef } }, '504941' => undef, '504944' => undef };

In other words, the node => {... is extra indirection that I don't need or want.

Any tips on how to coax this out of XML::Twig?


Dave


In reply to 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?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-25 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found