#!/usr/bin/perl use warnings; use strict; $|++; use XML::LibXML; my $parser = XML::LibXML->new(); my $closing_root_tag = ''; my $skip_past = ''; { $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 } }