http://qs321.pair.com?node_id=722667


in reply to xml::twig gathering all element and att and its value question

I agree with GrandFather here. In that specific case you don't use at all the fact that XML::Twig gives you trees. You are interested in the tags as such. XML::Parser should be enough for that.

  • Comment on Re: xml::twig gathering all element and att and its value question

Replies are listed 'Best First'.
Re^2: xml::twig gathering all element and att and its value question
by convenientstore (Pilgrim) on Nov 10, 2008 at 18:04 UTC
    but suppose I am already using xml::twig .. is it common for script to also use xml::parser in one script?

      Reread the links given in my previous reply and ask questions about that code if you need to. Ya ain't never gonna lern ifn ya don try!


      Perl reduces RSI - it saves typing
        #!/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