Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^4: xml::twig gathering all element and att and its value question

by convenientstore (Pilgrim)
on Nov 12, 2008 at 05:51 UTC ( #723062=note: print w/replies, xml ) Need Help??


in reply to Re^3: xml::twig gathering all element and att and its value question
in thread xml::twig gathering all element and att and its value question

#!/usr/bin/perl use warnings; use strict; use XML::Twig; 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 $result = ''; my $twig = XML::Twig->new ( twig_roots => { computer => sub { oneHandl +er ( \$result, @_, $yahoo);} } ); $twig->parse($xml); sub oneHandler { my ($result_ref, $twig, $elt,$yabal ) = @_; if ($elt->att('id') eq $yabal ) { #print $$result_ref, $elt->att('id') . "\n"; $elt->print; exit 1; } usage(); } sub usage { print "you suck\n"; exit 1; }
when I run it
<computer id="one" os="XP" type="mac"><lease true="yes"/><extra_device + value="scanner"/><entertainment><game company="nameco" id="tekken"/> +<platform value="pc only"/><year value="1980"/><game company="ninja" +id="tekken 2"/><platform value="pc and mac"/><year value="1989"/></en +tertainment></computer>
Now, I am trying to build a array of hash of hash reference such that
@array = { $key => { $key1 => $val } }
so for  <lease true="yes"/>
@array = { lease => { true => yes } }
I am stuck here.... I am not sure how to spit that output of the print so that I can stick them into the array the way I want

Replies are listed 'Best First'.
Re^5: xml::twig gathering all element and att and its value question
by GrandFather (Saint) on Nov 12, 2008 at 09:34 UTC

    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
      I thought i replied back but don't see my message so retyping
      first I really appreciate you writing back as I learn lot from you and also, perhaps I am still not writing my intention as best as I should(more example and code and clear explanation
      but I am trying very hard. I will give your solution more hack to see if I can make them do what I ultimately trying to go

      I am also trying out cookbook solution of
      my @nodes = $doc->findnodes("/books/book/authors/author/ firstname[text()='Tom']/../ lastname[text()='Chris']/ ../../../title/text()"); for my $node (@nodes) { print $node->data, "\n"; }

        Keep in mind that sample code in replies very often don't address your specific problem, but are intended to illustrate a technique that you can use to solve your problem. It often repays the time spent to go slowly through the code and make sure you understand each line. Often that may mean playing with the sample code to check your understanding. If you have a debugger it is often helpful to trace through parts of the code and examine the contents of variable to see that they are doing whet you expect.


        Perl reduces RSI - it saves typing

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2022-05-17 12:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (65 votes). Check out past polls.

    Notices?