Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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

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


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

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

Replies are listed 'Best First'.
Re^8: xml::twig gathering all element and att and its value question
by convenientstore (Pilgrim) on Nov 13, 2008 at 06:13 UTC
    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; } }
Re^8: xml::twig gathering all element and att and its value question
by convenientstore (Pilgrim) on Nov 16, 2008 at 02:48 UTC
    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?
        actually i did make it work
        my $sabal = new XML::Twig( twig_roots => { 'foo/yahoo' => #'bayking[@id="kingtony"]' => sub { my ($yabal, $element ) = @_; if ( $element->first_child('bayking_li +st')->first_child('bayking')->att('id') eq 'kingtony' ) { $element->print; } } } ); $sabal->parse($xml);
        result-->
        <yahoo V="baz"><bay_id><value>1000</value><fact>yes</fact></bay_id><ba +y_seen><value>50</value><fact>no</fact></bay_seen><bay_overall value= +"disabled"/><bayking_list><bayking active="true" country="Russia" id= +"kingtony"><bayking type="dictator"/><bay_usage value="none"/><baykin +g_origin><bayking_origin_name emmigrate="no" value="ohio_usa"><econom +y_status_previous value="very poor"/></bayking_origin_name></bayking_ +origin></bayking></bayking_list><bayqueen_list><bayqueen active="true +" country="japan" id="queensarah"><bayqueen type="dictator"/><bay_usa +ge value="none"/><bayqueen_origin><bayqueen_origin_name emmigrate="no +" value="ca_usa"/><economy_status_previous value="very poor"/><previo +us marriage="no"/></bayqueen_origin></bayqueen></bayqueen_list></yaho +o>
        now I just need to work on putting this on hash of hash refernce...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-16 11:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found