I'm UDPing an XML file and I want to parse it using XML::LibXML. After parsing then it must be validated against an XSD so that I know what type of XML file it is. But before all that I have to be able to handle broken XML files. I have noticed the
recover flag but what do I do with it? If i send a broken XML file through the stream I get the error:
Received message: n="1.0" encoding="UTF-8"?>
<Capabilities xsi:schemaLocation="http://Capabilities.CUIntegration.co
+m capabili
ties.xsd" xmlns="http://Capabilities.CUIntegration.com" xmlns:xsi="htt
+p://www.w3
.org/2001/XMLSchema-instance">
<VehicleID>String</VehicleID>
<System>
<ID>Servo</ID>
<AcceptedCommands>Loiter</AcceptedCommands>
<AvailableStreams>Telemetry</AvailableStreams>
</System>
</Capabilities>
:1: parser error : Start tag expected, '<' not found
n="1.0" encoding="UTF-8"?>
^
The document has no document element.
So now what? How do I fix this XML file so that it will validate ok?
Here are the codes:
Server
#!/usr/bin/perl -w
use strict;
use IO::Socket;
use XML::LibXML;
my $MySocket=new IO::Socket::INET->new(LocalPort=>1234,Proto=>'udp');
my $parser = XML::LibXML->new;
$parser->recover(1);
my $xml1 = "";
my $schema = XML::LibXML::Schema->new(location => 'C:\Users\deadpickle
+\Desktop\UAS\GRRUVI_1.50\panel\capabilities.xsd' );
while(1) {
if ($MySocket) {
$MySocket->recv($xml1,2669);
print "\nReceived message: ", $xml1,"\n";
my $doc = $parser->parse_string($xml1);
#$parser->validation(1);
eval { $schema->validate( $doc ) };
die $@ if $@;
print "VALID\n";
}
}
Client#!/usr/bin/perl -w
use strict;
use IO::Socket;
use File::Slurp;
my $xml1 = read_file("C:\\Users\\deadpickle\\Desktop\\UAS\\GRRUVI_1.50
+\\panel\\test.xml");
my $MySocket=new IO::Socket::INET->new(PeerPort=>1234,Proto=>'udp',Pee
+rAddr=>'localhost');
while (1) {
print $xml1;
$MySocket->send($xml1);
sleep 5;
}