Да, на самом деле это довольно просто. Ваш XML выглядит так: начинается с Data: [
и заканчивается на ] \ n \ n
Единственная проблема - если в XML есть скобка, он будет становится немного неловко.
В любом случае:
#!/usr/bin/env perl
use strict;
use warnings;
use XML::Twig;
#hardcoded. But could read this from the command line with
#e.g. shift @ARGV
#this will 'take' the first argument, but leave any others (e.g. filenames to read)
my $search_id = '089471386301077634';
#set record separator
local $/ = ">]\n";
#iterate the 'magic' filehandle.
# <> is either STDIN or files specified as args, just like "grep"
while ( <> ) {
#match Data followed by XML header. Multi-line
my ( $xml ) = m/Data:\[(\<\?xml[^]]+)/gms;
#if no match (e.g. not XML ) then skip
next unless $xml;
#parse it - pick up any errors (XML parse errors should be fatal
#hence the eval
my $twig = eval { XML::Twig -> parse ( $xml ); };
if ($@) {warn "XML blob couldn't be parsed $@\n"};
#set output formatting. (optional)
$twig -> set_pretty_print('indented_a');
#print it to STDOUT.
#but test if the 'equipment ID' is as required first:
if ( $twig -> findnodes("//ns12:equipmentId[string()=\"$search_id\"]") ) {
print "Found: \n";
$twig -> findnodes("//ns12:equipmentId[string()=\"$search_id\"]",0) -> print;
print "\nFull XML:\n";
$twig -> set_pretty_print('indented_a');
$twig -> print;
}
}