in reply to XML::Parser start handler
You should have a look at the various nodes about XML::Parser on the site (use the site search), starting with XML::Parser Tutorial and the review.
Here is a somewhat clean (except that it uses global variables) way to use XML::Parser for your purpose:
#!/usr/bin/perl -w use strict; use XML::Parser; my( $in_item, $item); my $parser= XML::Parser->new(); $parser->setHandlers( Start => \&start_handler, Char => \&char_handler, End => \&end_handler, ); $parser->parse( \*DATA); sub start_handler() # raises a flag when getting to Item { my ($p, $elmt ) = @_; if ( $elmt eq 'Item' ) { $in_item=1; $item=''; # reset the item text } } sub char_handler # stores the item text in $item { my( $p, $text)= @_; if( $in_item) { $item .= $text; } } sub end_handler # processes the item and lowers the flag { my ($p, $elmt ) = @_; if ( $elmt eq 'Item' ) { $in_item=0; # Toto, we are not in Item anymore # this is where you call the Item processing procedure print "item: $item\n"; } } __DATA__ <Orders> <Order ID="0008" Date="11/14/1999"><Item>A Book</Item></Order> </Orders>
Now as for your second question, XML::Parser's Subs style would help, or you could just use XML::Twig:
#!/usr/bin/perl -w use strict; use XML::Twig; my $twig= XML::Twig->new( twig_handlers => { Item => sub { print $_->t +ext, "\n"; } }); $twig->parse( \*DATA); __DATA__ <Orders> <Order ID="0008" Date="11/14/1999"><Item>A Book</Item></Order> </Orders>
BTW, I was confused by the use of Entity in your question. Entity already has a meaning in XML: < is an entity.
|
|---|