in reply to XML Twig - Isolate Element

The following seems to do what you want:

use strict; use warnings; use XML::Twig; my $nhf = XML::Twig->new( twig_roots => { RECORD => \&record_check, }, + ); $nhf->parse(<<'XML'); <EXPORT> <OUTPUT>2008-01-01</OUTPUT> <RECORD><ID>1</ID></RECORD> <RECORD><ID>2</ID></RECORD> </EXPORT> XML sub record_check { my ( $nhf, $record ) = @_; my $member = $record->first_child('ID')->text(); if ( $member < 1000 ) { $record->print(); print "\n"; } $nhf->purge (); }

Prints:

<RECORD><ID>1</ID></RECORD> <RECORD><ID>2</ID></RECORD>

Perl is environmentally friendly - it saves trees

Replies are listed 'Best First'.
Re^2: XML Twig - Isolate Element
by zacc (Novice) on Jan 03, 2008 at 17:35 UTC
    HMmm, I'm using 3.26 here (shoulda mentioned that earlier).

    I've tried "Print" but that repeats <EXPORT> for every line output... which is even worse.

    I'll upgrade to the latest and try again.

      Did you actually try the sample code? The output shown was with 3.26.

      Note that the print is called on $record, not $nhf.


      Perl is environmentally friendly - it saves trees
        Hmm, your code works fine.... even if I replace "parse" with "parsefile" and point in to my datafile.

        Gimme 10.