XML Validation

S

Shiraz

I am trying to use the XML simple to parse out some xml data. If I use
the code below with invalid xml, i just get a warning 'not well-formed
(invalid token) at line 1, column 16, byte 16 at /usr/local/lib/perl5/
site_perl/5.8.7/i686-linux/XML/Parser.pm line 187'
A test like 'unless (my $data = $xml->XMLin($msg) ) ' doesnt work
either.
Anyone know how to test for valid XML using just XML::Simple or would
i have to get a XML checking library

Thanks,

code:
#!/usr/bin/perl
use strict;
use XML::Simple;
$|=1;
my $xml = new XML::Simple;
my $msg = '<xml><select app>orig_gw</select></xml>'; #this is bad xml
my $data = $xml->XMLin($msg)

result:
not well-formed (invalid token) at line 1, column 16, byte 16 at /usr/
local/lib/perl5/site_perl/5.8.7/i686-linux/XML/Parser.pm line 187
 
M

Martien verbruggen

I am trying to use the XML simple to parse out some xml data. If I use
the code below with invalid xml, i just get a warning 'not well-formed
(invalid token) at line 1, column 16, byte 16 at /usr/local/lib/perl5/
site_perl/5.8.7/i686-linux/XML/Parser.pm line 187'
A test like 'unless (my $data = $xml->XMLin($msg) ) ' doesnt work
either.

This is not a warning. It's the XML::Simple module calling die() or one
of its equivalents, as documented.
Anyone know how to test for valid XML using just XML::Simple or would
i have to get a XML checking library

I believe you're asking how to deal with document errors using
XML::Simple? I'd suggest using eval() unless XML::Simple has something
better built in.

Since I have got nothing better to do right now, I had a look at the
documentation for XML::Simple, and you know what? It's got a section
called 'ERROR HANDLING', which contains the following paragraph:


If dying is not appropriate for your application, you should
arrange to call "XMLin()" in an eval block and look for errors in
$@. eg:

my $config = eval { XMLin() };
PopUpMessage($@) if($@);

Regards,
Martien
 
J

J. Gleixner

That is telling you it's invalid.

use XML::Simple;
use strict;

my $msg = '<xml><select app>orig_gw</select></xml>';
my $data;
eval {
my $xs = XML::Simple->new();
$data = $xs->XMLin( $msg );
};
print "Error: $@\n$msg\n";

There's something not right on the first line, column 16. You have to
figure out what's not correct.

[...]
One of them is available here, RxParse, a pure Perl xml,xhtml parser
that is excellent for that. Search the forum or google. I think the
one posted was version 1.1

Please, don't even suggest people use that module. Search the newsgroup
to see why.

Use something more reliable, like XML::Validator::Schema, if you have
an XSD.
 
J

J. Gleixner

Validation only works on the contents of well formed XML.
"<xml><select app>orig_gw</select></xml>" is not valid and has a single element.

I think that you are not even aware of xml is quite evident.

And I think that you are not even aware of English is quite evident.

Oh, and for future reference, it's XML.
To suggest to a poster with a problem,

Hey, leave my Farrah Fawcett poster out of this, there are no problems
with that poster.

that the person who gave the only good
advice, is wrong about recommending a particular solution, is quite out of line.

Possibly, if you actually tried educating yourself and tried it,
you'd see that it reports an error.

e.g.
Found unexpected <xml> inside <<<<ROOT>>>>. This is not a valid child
element.
I find your remarks insulting and not worth seeing you again!

I didn't even know we were seeing each other. I'm crushed.

Insulting hu... "Your mother was a hamster and your father smelt
of elderberries!". Now, there's something insulting.

Too funny... I am so honored. YOU publicly declaring a *plonk* - like
anyone cares.

Buh-Bye
 
J

John W. Kennedy

Sherm said:
Why - is Google too complicated for you?

Actually, "search Google Groups" really isn't an adequate answer. There
are too many postings about RxParse there, too many of them being
personal snarking and exchanges of "RxParse sucks!", "No it doesn't!",
"Yes it does!" You really have to give something to narrow it down.

--
John W. Kennedy
"Though a Rothschild you may be
In your own capacity,
As a Company you've come to utter sorrow--
But the Liquidators say,
'Never mind--you needn't pay,'
So you start another company to-morrow!"
-- Sir William S. Gilbert. "Utopia Limited"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top