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

If you have no control over your XML format and you are stuck using XML::Parser, then you can igonre the rest of this post. Otherwise, please consider the following.

While I'm sure what you have is valid XML, it's too bad it isn't structured differently. Had your CustomDimensionName and customBucketValueString elements been children of customBucket, you could have filtered your information as follows:

use strict; use warnings; use XML::Twig; my $xmlStr = <<XML; <foo> <customBucket> <customDimensionName>Strategy</customDimensionName> <customBucketValueString>Test1</customBucketValueString> </customBucket> <customBucket> <customDimensionName>SubStrategy</customDimensionName> <customBucketValueString>Test2</customBucketValueString> </customBucket> </foo> XML my $t = XML::Twig->new(); $t->parse($xmlStr); for my $bucket ($t->root()->children('customBucket')) { if ($bucket->first_child('customDimensionName')->text() ne 'Strate +gy') { print 'customDimensionName ' , $bucket->first_child('custom +DimensionName' )->text(), "\n"; print 'customBucketValueString ', $bucket->first_child('custom +BucketValueString')->text(), "\n"; } } __END__ customDimensionName SubStrategy customBucketValueString Test2

I abandoned using XML::Parser once I discovered XML::Twig, which I find to be easier to understand and use.

  • Comment on Re: How to exclude certain blocks of an XML file using Perl and XML::Parser
  • Download Code

Replies are listed 'Best First'.
Re: How to exclude certain blocks of an XML file using Perl and XML::Parser
by kgullekson (Initiate) on Feb 12, 2009 at 23:28 UTC
    Thanks for the info. Yeah, I know the format is a bit bizarre. Plus, I just checked and I don't have XML::Twig available (it looks like it has much more functionality). I appreciate your response.

      XML::Twig is pure Perl, so you can just stick Twig.pm (from the XML-Twig distribution) somewhere, add a use lib 'somewhere'; in your code, and use it.