Thinking about this one more time ... in case the question actually is not "is the XML valid", but rather "is the XML already complete, assuming it's being uploaded from a valid source" then the XML parser modules based solutions are an overkill. For a quick check with minimal memory footprint something like this may be better:

sub XMLisComplete { my $file = shift(); open my $IN, '<', $file or return; # if I can't open it, it's prob +ably locked. Therefore it's not complete. my $main_tag; read $IN, $main_tag, 1024; if ($main_tag =~ m{<(\w+)}) { $main_tag = $1; } else { return; # there's not even the opening tag! } seek $IN, 2, -100; my $end = do {local $/; <$IN>}; close $IN; if ($end =~ m{</$main_tag>\s*$}s) { return 1; } else { return; } }

It's most likely not 100% standard proof (e.g. I bet \w+ doesn't match all allowed tag names), but it works for me to test whether I can already start parsing the uploaded file or whether to wait a bit more. The actual parsing would of course be best left to a module.


In reply to Re: Regular Expression XML Searching Help by Jenda
in thread Regular Expression XML Searching Help by Anonymous Monk

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.