Dear Beginners List,

My code is based on an example from the XML::Twig documentation.  I want to
capture the text of a couple elements that have descendant elements and put
this text in separate variables (one for each element's text).  The problem
in the code example is that all the text of the lower elements is returned
in a 'print'.  So to illustrate:

<title>text                       # I want this piece of text saved in one
variable
<subtitle>more text          # and this piece of text stored in another
variable
<p>some more text</p>   # without getting this
</subtitle>
</title>

Code:
---------------------------------------------
 use strict;
 use warnings;
 use XML::Twig;


 my $t= XML::Twig->new(
 # the twig will include just the root and selected titles
         twig_roots   => { 'title' => \&writeDBrow,
                                  'subtitle' => \&writeDBrow
         }
         );
 $t->parsefile( 'doc.xml');

 sub writeDBrow
 { my( $t, $elt)= @_;
     print $elt->text;    # print the text (including sub-element texts)

# Insert Code to write to a Database here

          $t->purge;           # frees the memory
 }
---------------------------------------------

Thanks,

Jon

Reply via email to