XML Validation

Discussion in 'Perl Misc' started by Shiraz, Jul 25, 2007.

  1. Shiraz

    Shiraz Guest

    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
     
    Shiraz, Jul 25, 2007
    #1
    1. Advertising

  2. On Wed, 25 Jul 2007 14:56:36 -0700,
    Shiraz <> wrote:
    > 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
    --
    |
    Martien Verbruggen | It's not what we don't know that hurts us,
    | it's what we know for certain that just ain't
    | so. -- Mark Twain
     
    Martien verbruggen, Jul 26, 2007
    #2
    1. Advertising

  3. Shiraz

    J. Gleixner Guest

    wrote:
    > On Wed, 25 Jul 2007 14:56:36 -0700, Shiraz <> wrote:
    >
    >> 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


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

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


    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. Gleixner, Aug 2, 2007
    #3
  4. J. Gleixner <> wrote:
    > wrote:



    >> One of them is available here, RxParse, a pure Perl xml,xhtml parser
    >> that is excellent for that.


    > Please, don't even suggest people use that module.



    I expect that sln is the author of that module.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad McClellan, Aug 2, 2007
    #4
  5. Shiraz

    J. Gleixner Guest

    wrote:
    > On Thu, 02 Aug 2007 10:31:40 -0500, "J. Gleixner" <> wrote:
    >
    >> wrote:
    >>> On Wed, 25 Jul 2007 14:56:36 -0700, Shiraz <> wrote:
    >>>
    >>>> I am trying to use the XML simple to parse out some xml data. If I use


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

    >
    > 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.


    > *plonk*

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

    Buh-Bye
     
    J. Gleixner, Aug 3, 2007
    #5
  6. Sherm Pendley wrote:
    > writes:
    >
    >> On Thu, 02 Aug 2007 20:49:49 -0400, Sherm Pendley <> wrote:
    >>
    >>> writes:
    >>>
    >>>> 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
    >>> The only thing Robic0's module is good for is to serve as a bad example.
    >>> Go ahead and search Google Groups for it though - you'll find out what's
    >>> wrong with it quickly enough.

    >> Yeah, can you describe whats wrong with it

    >
    > 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"
     
    John W. Kennedy, Aug 3, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page