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; }

In reply to repairing a broken xml using LibXML by deadpickle

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.