Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

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

It's not clear to me what you are trying to achieve here (assigning a hash ref to an array doesn't make sense), but the following may help:

use warnings; use strict; use XML::Twig; use Data::Dump::Streamer; my $xml = <<XML; <config> <computer id="one" type="mac" os="XP" > <lease true="yes" /> <extra_device value="scanner"/> <entertainment> <game id="tekken" company="nameco" /> <platform value="pc only" /> <year value="1980" /> <game id="tekken 2" company="ninja" /> <platform value="pc and mac" /> <year value="1989" /> </entertainment> </computer> <computer id="two" type="pc" os="NT" > <lease true="no" /> <work> <software value="final" /> </work> </computer> <company_name id="nameco" origin="ca" type="violence" production="ye +s"> <sponsor name="sony" percentage="30" active="true"/> <sponsorlist> <sponsor id="sony1" active="false"/> <sponsor id="sony2" active="true"/> <sponsor id="sony3" active="false"/> </sponsorlist> </company_name> <company_name id="ninja" origin="ny" type="extreme violence" product +ion="yes"> <sponsor name="WB" percentage="20" active="true"/> <sponsorlist> <sponsor id="WB1" active="false"/> </sponsorlist> </company_name> </config> XML my $yahoo = 'one'; my @ones; my $twig = XML::Twig->new ( pretty_print => 'indented', twig_roots => { computer => sub {oneHandler (\@ones, $yahoo, @_);} } ); $twig->parse ($xml); Dump (\@ones); sub oneHandler { my ($ones, $yabal, $twig, $elt) = @_; return unless $elt->att ('id') eq $yabal; for my $child ($elt->children ()) { my %atts = %{$child->atts ()}; next unless %atts; push @$ones, \%atts; } }

Prints:

$ARRAY1 = [ { true => 'yes' }, { value => 'scanner' } ];

Perl reduces RSI - it saves typing

In reply to Re^5: xml::twig gathering all element and att and its value question by GrandFather
in thread xml::twig gathering all element and att and its value question by convenientstore

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 learning in the Monastery: (5)
As of 2024-03-28 19:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found