Davorg has good advice here. I asked the
very same question a while back.
Podmaster beat
mirod to the punch plugging
XML::Twig, which I am using in production today :-) That node also has a simple SAX version (that I'm still not quite comfortable with).
It should be noted that the first two solutions above are brittle, and will break on things such as nested <ArgusFlowRecord> tags in a CDATA section.
So, take
davorg's advice. Use a real parser. But if you don't, here's a little snippet that I use when speed is of the essence. It's probably broken in some way that I haven't realized yet (and some that I have). It expects its input from STDIN.
#!/usr/bin/perl
use warnings;
use strict;
$|++;
use XML::LibXML;
my $parser = XML::LibXML->new();
my $closing_root_tag = '</ArgusFlowRecord>';
my $skip_past = '<ArgusDataStream>';
{
$XML::LibXML::skipXMLDeclaration = 1;
local $/ = $skip_past;
<>; # ignore declaration and stream open tag
local $/ = $closing_root_tag;
my $temp_chunk;
while ( <> ) {
$temp_chunk .= $_;
my $dom;
eval { $dom = $parser->parse_string( $temp_chunk ) };
next if $@; # keep nibbling if XML is invalid
undef $temp_chunk;
print $dom->toString(), "\n"; # or do other processing
}
}
-- dug