Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: (YA) Perl XML parser

by mirod (Canon)
on May 07, 2002 at 05:33 UTC ( #164528=note: print w/replies, xml ) Need Help??

in reply to (YA) Perl XML-like parser

The title of your post should be YA piece of code that parses a limited and undefined subset of XML.

XML parsers can be validating (against a DTD) or not (they just test well-formedness), see the XML spec. But a non-validating parser still must cover the whole spec. Nothing is optional in the XML spec. To claim to be an XML parser your code must be able to deal with entities, CDATA sections, comments, PI's...

A couple more cases that your code does not handle:

  • <doc att="toto>"/>: yes > is legal in a tag, if it is in an attribute value,
  • <doc><section><section></section></section></doc>: nested tags are perfectly legal and widely used, so you can't use a regexp like <tag>.*?</tag> like you do to match an entire element.

You should really study XML::SAX::PurePerl for a real XML parser in pure Perl (XML::SOAP::Lite does not parse all of XML, just the subset used by SOAP).

Once again: writing something that parses the specific XML data you have to deal with at the moment is usually quite easy, but writing a real XML parser is _HARD_. You can of course use this parser for your data, but try to remember that it does not parse all of XML, and do not complain if one day it breaks on perfectly valid XML.

And please do not post the code and above all do not pretend it is an XML parser. You are doing a disservice both to other monks and to you.

OK, I think I am done. I will now take off my XML ayatollah hat and resume my normal activities ;--)

Replies are listed 'Best First'.
Re: Re: (YA) Perl XML parser
by belg4mit (Prior) on May 07, 2002 at 06:50 UTC
    Did you run the code? Did you read the comments? It handles nested tags, you mean a tag nested within itself, they are parsed but a level is lost. PI won't be supported. I shall endeavor to handle > in the attributes and CDATA.

    I did not see XML::SAX::PurePerl in my search earlier, oh well.

    The XML spec is far from the easiest thing to read. I appreciate the insight, and am filling in most of the gaps but this is going to be like perl52perl6, 100%, 80% of the time. It wasn't even the original intent. It started as just an RSS parser but seemed to handle generic XML and so It thought I'd share it. Other things to keep in mind are; I did in fact say it was lax, this is snippets not craft.

    perl -pew "s/\b;([mnst])/'$1/g"

      If I spent time debugging all the pseudo-xml parsers I come accross I would not have much time left for real work. That said you are right, your thing handles nested tags just fine. It does not handle mixed content though.

      The problem is not which specific feature your code does or does not handle, it is that you have no idea what portion of the XML spec it covers. As you said the XML spec is complex and hard to read. It includes a grammar though, and that's how you should tackle writing a parser: extract the grammar (Ways to Rome will give you 11 ways to do this) and work from there. Do not pretend that code that parses "stuff with pointy brackets" is an XML parser.

      There have been numerous discussions about this on this forum (parse the "Other Users XML Ticker" with index and substr being a recent one, On XML parsing giving you a bunch of features that make parsing difficult). It boils down to "do what you want at home, but please don't spread improper code".

      Now if you had worked on XML::RSS to get it to work with XML::SAX::PurePerl, now you would have written something useful for you (minimal dependance for an RSS parser) and for the rest of the World.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://164528]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2022-01-18 22:14 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (54 votes). Check out past polls.