Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

After reading a recent question, but also some older onesI thought it would be worth mentionning the basic rule of XML processing: Use a parser!

As I know you won't take my word for it I will give you just a couple of examples of things that might (that will) go wrong if you use plain regexps:

  • XML comments:

    <tag>value 1</tag> <!-- <tag>value 2</tag> --> <tag>value 3</tag>
    will probably hurt you first, then get you to write quite tricky regexps,

  • entities:

    <tag>value 1</tag> &v2; <tag>value 3</tag>
    what will your regexp do with the &v2; entity? Will it look in the appropriate place (right in the DTD, or in a separate file, maybe remote) to get the entity declaration: <!ENTITY v2 "<tag>value 2</tag>">

  • CDATA:

    <tag>value 1</tag> <tag><![CDATA[ <tag2>value 2</tag2> ]]></tag> <tag>value 3</tag>
    the data inside the CDATA should be treated literally, there is no tag2 element in the document,

  • namespaces:

    <mynamespace:tag>value 1</mynamespace:tag> <theirnamespace:tag>value 3</theirnamespace:tag>
    the 2 tag elements may or may not refer to the same element, depending on the namespace declarations in the document.

Not to mention the usual kind of problem with evolving XML, when the content of the tag element starts including additional mark-up, when the tag element gets a bunch of attributes, or when tag2 elements start popping up in between tag elements.

You might think that you don't care about all of those, your XML is simple and you don't need no stinkin' namespaces. WRONG! You are limiting yourself to a subset of XML, but you are NOT calling it a subset. And either you or (pity them!) the people who will maintain your code won't remember that it is only a subset, and what subset. Plus you might have total control over this pseudo-XML today but tomorrow? Maybe you will receive it from some external source, or you will use an off-the-shelf tool to create it.

Plus those extra features that your lovingly crafted regexps don't grok might come in handy in the future, will you add them to your software? Will you end up writing your own regexp-based parser? It has been done by the way, it's just that XML::Parser is faster for non-trivial XML, and I happen to trust James Clark more than myself when it comes to writing a parser.

So please, anytime you want to process XML, especially if the software is going to be used for a while, please,

Use the Parser Luke!


In reply to On XML parsing by mirod

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (7)
    As of 2019-12-10 20:59 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?