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

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"; }
  • Comment on Re^6: xml::twig gathering all element and att and its value question
  • Download Code

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

    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);
        I was hoping this would work but still no go..
        Why (what made you think it would work)? What errors do you get?