in reply to Re: XML::Twig's twig_print_outside_roots adds extra blank lines to output
in thread [SOLVED] XML::Twig's twig_print_outside_roots adds extra blank lines to output
Thank you very much Remiah! That was exactly what I was looking for.
For those that come after, here is the final code I used to get my desired output:
#!/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 line +s ## my $t = XML::Twig->new( twig_handlers => { 'statement' => sub { _statement(@_, $OFILE); 1; }, 'statement//*' => sub { 1; }, # skip descendants of statem +ent '_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_t +ext(); if ( $acct eq '903264' ) { $_twig->flush($OFILE); } else { $_twig->purge(); } return; } __DATA__ <batch> <header> <foo>1</foo> <bar>2</bar> <baz>3</baz> </header> <statement> <primary> <acct>000000</acct> </primary> <bits>Im an apple!</bits> </statement> <statement> <primary> <acct>000000</acct> </primary> <bits>Im a carrot!</bits> </statement> <statement> <!-- I only want this statement --> <primary> <acct>903264</acct> </primary> <bits>Im a potato!</bits> </statement> <statement> <primary> <acct>000000</acct> </primary> <bits>Im a pear!</bits> </statement> <statement> <primary> <acct>000000</acct> </primary> <bits>Im a pickle!</bits> </statement> <statement> <primary> <acct>000000</acct> </primary> <bits>Im a banana!</bits> </statement> <statement> <primary> <acct>000000</acct> </primary> <bits>Im an eggplant!</bits> </statement> <trailer> <stuff>Oh god how did this get here i am not good with compute +r!</stuff> </trailer> </batch>
|
|---|