#!/usr/bin/perl use 5.016; use strict; use warnings; use XML::Twig; { open (my $OFILE, '>:utf8', './bits.xml') or die "Could not open [./bits.xml]:\n$!\n$^E"; ## ## An aproximation of 'twig_roots' and 'twig_print_outside_roots' that ## keeps pretty printing and does not print superfluous blank lines ## my $t = XML::Twig->new( twig_handlers => { 'statement' => sub { _statement(@_, $OFILE); 1; }, 'statement//*' => sub { 1; }, # skip descendants of statement '_default_' => sub { $_[0]->flush($OFILE); 1; }, # just flush }, pretty_print => 'indented', ); $t->parse(*DATA); close $OFILE; } sub _statement { my ($_twig, $stmt_element, $OFILE) = @_; my $acct = $stmt_element->find_nodes('primary/acct', 0)->trimmed_text(); if ( $acct eq '903264' ) { $_twig->flush($OFILE); } else { $_twig->purge(); } return; } __DATA__
1 2 3
000000 Im an apple! 000000 Im a carrot! 903264 Im a potato! 000000 Im a pear! 000000 Im a pickle! 000000 Im a banana! 000000 Im an eggplant! Oh god how did this get here i am not good with computer!