in reply to How to exclude certain blocks of an XML file using Perl and XML::Parser

If you can go the XML::Twig way, here is a piece of code that would work:

#!/usr/bin/perl use strict; use warnings; use XML::Twig; XML::Twig->new( twig_handlers => { customBucketEnd => \&end_bucket }, pretty_print => 'indented', ) ->parse( \*DATA) ->flush; # assumes the customBucket and customDimensionName elements are ALWAYS + present sub end_bucket { my( $t, $end)= @_; my @bucket_content; if( $end->prev_sibling( 'customDimensionName')->text eq 'Strategy' +) { while(1) { $end->cut; if( $end->tag eq 'customBucket') { last; } $end= $end->former_prev_sibling; # an obscure method that c +omes in handy sometimes } } else { $t->flush; } } __DATA__ <doc> <customBucket></customBucket> <customDimensionName>Strategy</customDimensionName> <customBucketValueString>Test1</customBucketValueString> <customBucketEnd></customBucketEnd> <customBucket></customBucket> <customDimensionName>SubStrategy</customDimensionName> <customBucketValueString>Test2</customBucketValueString> <customBucketEnd></customBucketEnd> </doc>
  • Comment on Re: How to exclude certain blocks of an XML file using Perl and XML::Parser
  • Download Code