in reply to Regular Expression XML Searching Help
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.
|
|---|