in reply to Re^2: I don't understand why I'm getting an "Use of uninitialized value" error
in thread I don't understand why I'm getting an "Use of uninitialized value" error
Exactly that is demonstrated in the example for the section Processing just parts of an XML document. One way of achieving that is:
#!/usr/bin/perl -w use warnings; use strict; use XML::Twig; my $xml = <<XML; <head><ID>This is an id</ID> <Book1> <Title> Title stuff </Title> </Book1> <Book2><Title>Another title</Title></Book2> </head> XML my $twig = XML::Twig->new( twig_roots => { ID => \&dump, TimeStamp => \&dump, IP_Address => \&dump, 'Book1/Title' => sub {title (1, @_);}, 'Book2/Title' => sub {title (2, @_);}, Complainant => \&dump, } ); $twig->parse($xml); sub dump { my ($t, $elt) = @_; (my $text = $elt->text()) =~ s/^\s+|\s+$//g; print "$text\n"; } sub title { my ($type, $t, $elt) = @_; (my $title = $elt->text()) =~ s/^\s+|\s+$//g; print "Title $type: $title\n"; }
Prints:
This is an id Title 1: Title stuff Title 2: Another title
Take this code. Make sure you have XML::Twig installed. Play with it until you have some understanding of how it works.
|
|---|