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

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

by GrandFather (Saint)
on Nov 12, 2008 at 09:34 UTC ( [id://723087]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^6: xml::twig gathering all element and att and its value question
by convenientstore (Pilgrim) on Nov 12, 2008 at 19:49 UTC
    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
        yes definitely understand
        I am trying below so that I can find all element/att/value .. so far no luck but will keep digging
        my $yahoo = 'one'; my @ones; my $twig = XML::Twig->new ( twig_roots => { computer => sub { oneHandl +er ( \@ones, @_, $yahoo);} } ); $twig->parse($xml); print Dumper(@ones); sub oneHandler { my ($result_ref, $twig, $elt,$yabal ) = @_; my %master; return unless $elt->att('id') eq $yabal; for my $child ( $elt->descendants() ) { #my %atts = %{ $child->atts () }; $master{$child->child} = %{ $child->atts () }; next unless %master; push @$result_ref, \%master; } }
        I was hoping this would work but still no go..
        use strict; use warnings; use XML::Twig; my $xml = <<XML; <foo> <yahoo V="bay"> <bay_id> <value>1</value> <fact>yes</fact> </bay_id> <bay_seen> <value>10</value> <fact>no</fact> </bay_seen> <bay_overall value="disabled"/> <bayking_list> <bayking id="kingjames" country="usa" active="true"> <bayking type="dictator"/> <bay_usage value="none"/> <bayking_origin> <bayking_origin_name value="ohio_usa" emmigrat +e="no"> <economy_status_previous value="very po +or" /> </bayking_origin_name> </bayking_origin> </bayking> </bayking_list> <bayqueen_list> <bayqueen id="queenliz" country="france" active="true"> <bayqueen type="dictator"/> <bay_usage value="none"/> <bayqueen_origin> <bayqueen_origin_name value="ohio_usa" emmigra +te="no"/> </bayqueen_origin> </bayqueen> </bayqueen_list> </yahoo> <yahoo V="baz"> <bay_id> <value>1000</value> <fact>yes</fact> </bay_id> <bay_seen> <value>50</value> <fact>no</fact> </bay_seen> <bay_overall value="disabled"/> <bayking_list> <bayking id="kingtony" country="Russia" active="true"> <bayking type="dictator"/> <bay_usage value="none"/> <bayking_origin> <bayking_origin_name value="ohio_usa" emmigrat +e="no"> <economy_status_previous value="very po +or" /> </bayking_origin_name> </bayking_origin> </bayking> </bayking_list> <bayqueen_list> <bayqueen id="queensarah" country="japan" active="true"> <bayqueen type="dictator"/> <bay_usage value="none"/> <bayqueen_origin> <bayqueen_origin_name value="ca_usa" emmigrate +="no"/> <economy_status_previous value="very po +or" /> <previous marriage="no"/> </bayqueen_origin> </bayqueen> </bayqueen_list> </yahoo> </foo> XML my $sabal = new XML::Twig( twig_roots => { 'foo/yahoo' => #'bayking[@id="kingtony"]' => sub { my ($yabal, $element ) = @_; if ( $yabal->bayking_list->bayking->att(' +id') eq 'kingtony' ) { $element->print; } } } ); $sabal->parse($xml);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (1)
As of 2024-04-24 16:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found