meorero has asked for the wisdom of the Perl Monks concerning the following question:

Hello all.

I have an XML file as follows:

<messages> </messages>
inside it - <message> with attribute ID.
For each <message>
- one <message_id>,
- one <message_desc>,
- and ... many elements / parameters - each one inside <row>...</row> tags.

What I want is:
- Show the user all message ID's and Desc,
- Ask the user to choose an ID,
- and then: show the user and ask him / her about each of the elements / parameters (from the <row></row> tags) - of the *chosen message*.

I managed - with XML::Twig - to do the first two: show the message ID's and Desc, and get the message ID from the user.

But how do I handle each of the elements / parameters from the <row></row> tags? I want to show it...

Thanks.

Here is my code:

#!/usr/bin/env perl use strict; use warnings; use XML::Twig; ################################################# ################################################# my $twig_show_messages = new XML::Twig( twig_handlers => # message will be called { message => \&print_all_messages_id_desc } # when each message +element ); # has been parsed $twig_show_messages->parsefile("sip2_params_client_small.xml"); # +build the twig #### $twig_show_messages->flush; ######################################## ######################################## print "Your SIP2 message ID?>_\t"; my $match_msg=''; chomp ($match_msg=<> ); ## Check the input if ($match_msg !~ /^(-)*\d{2}$/){ ## Input must be 2 digits! If not.. +. exit! print "***===>>>input $match_msg. \n"; print "\tIt is not numeric, or does *not* contain two digit\n"; exit; } ######################################## ######################################## my $twig_for_a_message= new XML::Twig(); ######################################## ######################################## my $twig_the_selected_message= new XML::Twig( twig_handlers => { doc => \&doc, section => \&section, } ); ######################################## ######################################## &print_chosen_msg; ################################################# ################################################# ################################################# ################################################# ################################################# ################################################# sub print_all_messages_id_desc { my( $twig, $message)= @_; # handlers params ar +e always # the twig and the e +lement my $message_id = $message->first_child('message_id')->text; # g +et the text of message_id my $message_desc = $message->first_child('message_desc')->text; # +get the text of message_desc print "$message_id\t$message_desc\n"; # print message id and + desc } ################################################# ################################################# sub print_chosen_msg { my $sw_msg_flag= ' '; print "You chose:\t"; ### START XML::Twig XML::Twig->new( twig_handlers => { ## START message_id message_id => sub { if ($_->text eq $match_msg) { print $_->text ; print "\t"; $sw_msg_flag='Y'; } ## END IF }, ## END message_id message_desc => sub { if ($sw_msg_flag eq 'Y') { print "\n"; print $_->text ; print "\n"; $sw_msg_flag='N'; # exit; } ## END IF } ## END message_desc } ## END twig_handlers )->parsefile('sip2_params_client.xml'); ### END XML::Twig print "\t==============================\n"; }

Example of the XML:

<?xml version="1.0" encoding="UTF-8"?> <messages> <message id ="23"> <!-- <row> --> <message_id>23</message_id> <message_desc>Patron Status Request</message_desc> <ver/> <!-- <field_desc></field_desc> --> <field_id/> <format/> <!-- </row> --> <row> <ver/> <field_desc>language </field_desc> <field_id/> <format>3-char, fixed-length required field</format> </row> <row> <ver/> <field_desc>transaction date</field_desc> <field_id/> <format>18-char, fixed-length required field: YYYYMMDDZZZZHHMMSS< +/format> </row> <row> <ver/> <field_desc>institution id </field_desc> <field_id>AO</field_id> <format>variable-length required field</format> </row> <row> <ver/> <field_desc>patron identifier</field_desc> <field_id>AA</field_id> <format>variable-length required field</format> </row> <row> <ver/> <field_desc>terminal password</field_desc> <field_id>AC</field_id> <format>variable-length required field</format> </row> <row> <ver/> <field_desc>patron password </field_desc> <field_id>AD</field_id> <format>variable-length required field</format> </row> <!-- message end --> </message> <message id="09"> <!-- <row> --> <message_id>09</message_id> <message_desc>Checkin</message_desc> <ver/> <!-- <field_desc></field_desc> --> <field_id/> <format/> <!-- </row> --> <row> <ver/> <field_desc>no block</field_desc> <field_id/> <format>1-char, fixed-length required field: Y or N.</format> </row> <row> <ver/> <field_desc>transaction date</field_desc> <field_id/> <format>18-char, fixed-length required field: YYYYMMDDZZZZHHMMSS< +/format> </row> <row> <ver/> <field_desc>return date</field_desc> <field_id/> <format>18-char, fixed-length required field: YYYYMMDDZZZZHHMMSS< +/format> </row> <row> <ver/> <field_desc>current location</field_desc> <field_id>AP</field_id> <format>variable-length required field</format> </row> <!-- message end --> </message> <!-- </sip2_client_send> --> </messages>

Replies are listed 'Best First'.
Re: XML::Twig - how to get and display only selected elements?
by Anonymous Monk on Nov 26, 2011 at 18:52 UTC

    Well, you should write some functions, and pass them arguments, and they should either do something ( like print stuff) or return values

    That way, you can test each in function isolation, independent of your program

    Then, when you show us sample xml, and ask

    But how do I handle each of the elements / parameters from the <row></row> tags? I want to show it...

    you can also show us the input this function takes (say a twig and some xpaths, or a xmlfile and some xpaths, or twig/file and target-id), and the output or return value, you expect, but are having trouble achieving

    These should be of use with the task: XML:: Twig - can you check for text following the element being handled?, XML::Twig n00b , in combination with Term::Interact and Term::Interact example