tags:

views:

195

answers:

6

Hi all,

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 arrays and extract the data from that point?

Thanks in advance! :)

+6  A: 

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
  • ...
svens
+13  A: 

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

VolkerK
+6  A: 

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.

efritz
+10  A: 

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.

Alan Storm
Thanks for the explanation.
esqew
A: 

Yes ,I also have the question is 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.

panasonic lumix dmc-zx1 batter
+1  A: 

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.

BuckWild