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

In reply to Re: Record based XML stream processing? by dug
in thread Record based XML stream processing? by fs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.