开发者

Why use an XML parser?

I'm a somewhat experienced PHP scripter, however I just dove into parsing XML and all that good stuff.

I just can't seem to wrap my head around why one would use a separate XML parser instead of just using the explode function, which seems to be just as simple. Here's what I've been doing (assuming there is a valid XML file at the path xml.php):

$contents = file_get_contents("xml.php");
$array1 = explode("<a_tag>", $contents);
$array2 = explode("</a_tag>", $array1[1]);
$data = $array2[0];

So my question is, what is the practical use for an XML parser if you can just separate the values into ar开发者_如何转开发rays and extract the data from that point?

Thanks in advance! :)


Excuse me for not going into details but for starters try parsing

$contents = '<a xmlns="urn:something"> 
  <a_tag>
    <b>..</b>
    <related>
      <a_tag>...</a_tag>
    </related>
  </a_tag>
  <foo:a_tag xmlns:foo="urn:something">
    <![CDATA[This is another <a_tag> element]]>
  </foo:a_tag>
</a>';

with your explode-approach. When you're done we can continue with some trickier things ;-)


In a nutshell, its consistency. Before XML came into wide use there were numerous undocumented formats for keeping information in files. One of the motivators behind XML was to create a well defined, standard document format. With this well defined format in place, a general set of parsing tools could be developed that would work consistently on documents so long as the documents adhered to the aforementioned well defined format.

In some specific cases, your example code will work. However, if the document changes

...
<!-- adding an attribute -->
<a_tag foo="bar">Contents of the Tag</a_tag>
...

...
<!-- adding a comment to the contents -->
<a_tag>Contents <!-- foobar --> of the Tag</a_tag>
...

Your parsing code will probably break. Code written using a correctly defined XML parser will not.


XML parsers:

  • Handle encoding
  • May have xpath support
  • Allow you to easily modify and save the XML; append/remove child nodes, add/remove attributes, etc.
  • Don't need to load the whole file into memory (except from DOM parsers)
  • Know about namespaces
  • ...


How would you explode the same file if a_tag had an attribute?

explode("<a_tag>" ... will work differently than explode("<a_tag attr='value'>" ..., after all.

XML Parsers understand the XML specification. Explode can only handle the simplest of cases, and will most likely fail in a lot of instances of that case.


Using a proven XML parsing method will make the code more maintainable and easy to read. It will also make it more easily adaptable should the schema change, and it can make it easier to determine error conditions. XPath and XSLT exist for a reason, they are proven ways to deal with XML data in a sensible, legible manner. I'd suggest you use whichever is applicable in your given situation. Remember, just because you think you're only writing code for one specific purpose, you never know what a piece of well-written code could evolve into.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜