in reply to Re^5: XML::Twig question
in thread XML::Twig question

Yea, same guy. Had problems at home with other user then use bizza user.

Belows what I've done. Firstly, I found out that the inner elements <ID> are processed before the outer ones <AC>. With the twig_handlers, all of them are being processed eventhough, for e.g you only want a listing of <AC>'s. I think I'll have if stmts to check what the users requesting.

The tutorial is too lightweight, xml examples doesn't really deal or include examples attribute checking/extraction. Actually, the xml file used(nba.xml)to simple, hence the simple examples.

Also some XML::XPath usage would be great. This doesn't come thru' even in the developer version document. I think the tutorial should include a comprehensive xml file with attributes. I should show how extract and/or update specific elements (attribs and tags), how to find specific nodes, delete, modify and update em, traverse the doc. Because of the myriad of methods, classes and/or options available to perform a function - in maybe both "normal twiggy way" and xpath way.

There's a lotta things to say, however that's one for another topic. The way it is, to pick XML::Twig is really difficult

Bizza(thandi)

HERE're my attempts at getting to know and understand: #!/usr/bin/perl use strict; use warnings; use XML::Twig; # perl test_thandi CCC '@n=AAA' # perl test_thandi CCC '@n="AAA" or @set="y"' my( $ac_n_value, $ai_att_cond)= @ARGV; XML::Twig->new( TwigHandlers => { # qq{AC[\@n="$ac_n_value"]/AI[$ai_att_cond]} => \&print_ai_data }, # qq{AC[\@n="$ac_n_value"]/AI[$ai_att_cond]/ID[\@n=$id_att_cond]} => + \&print_id_data }, # ID => \&id_print , #list all IDs desc <desc> </desc> # AI => \&ai_print , qq{AC} => \&ac_print, #list all ACs <desc> </desc> qq{AC/AI} => \&ai_print, #list all AIs <desc> </desc> }, pretty_print => 'indented', # keep_atts_order => 1, # do_not_chain_handlers => 1 ) ->parsefile( "custs.xml"); sub ac_print { my( $t, $ac)= @_; my $Desc = $ac->first_child('Desc')->text; print "AC DESC: $Desc\n"; print "=======================================\n"; } sub ai_print { my( $t, $ai)= @_; my $Desc = $ai->first_child('Desc')->text; print "AI DESC: $Desc\n"; print "----------------------------------\n"; } sub id_print { my( $t, $id)= @_; my $Desc = $id->first_child('Desc')->text; my $What = $id->first_child('What')->text; my $AR = $id->first_child('AR')->text; print "--> DESC: $Desc\n"; print "What: $What\n"; print "AR: $AR\n"; }

Replies are listed 'Best First'.
Re^7: XML::Twig question
by mirod (Canon) on Dec 20, 2006 at 13:35 UTC

    Working with attributes: the att and set_att methods are described in the tutorial: "4.6 A simple HTML+ converter" in http://xmltwig.com/xmltwig/tutorial/yapc_twig_s4.html.

    An XPath tutorial is well beyond the scope of the XML::Twig documentation, a good one can be found at zvon.org: http://zvon.org/xxl/XPathTutorial/General/examples.html

    The tutorial shows the cut method (it could also show thedelete method and explain the difference between the 2, I have added that to the text), it also explains how to create new elements, and how to navigate the tree.

    I have also added an explanation of the order in which the handlers are called.

    So I have updated the tutorial based on your remarks.

    Thanks for your help.

      Thanks a stack. Will check out the updated tutorial(hope it's the same one I've been reading). I've also checkout the XPath tutorial. Problem is that I need to get a grasp of what the twig methods return and how to manipulate em.

      thanks