Event-based XML parsing, at last!

Parsing XML takes two steps. The second, harder part, we've already covered - working through the data with Expat. The first step is much easier, which is why I left it till last - you just need to read the contents of your selected XML file into a string, then pass the string to PHP.

Before you begin, one last note: a special option in PHP's XML parsing implementation, "Case Folding", is set to true by default. Case folding is basically the process of automatically converting the names of elements and the names of attributes to uppercase characters, and it is enabled by default. You'd do well to remember this when you are wondering why all the elements passed to your element handler function are uppercased!

Onto the code itself...

    $file = '/path/to/somexmlfile.xml';
    if (!file_exists($file)) {
        print "Error loading XML file - please check the file exists and that you have access to it.";
    } else {
        print "XML file loaded successfully!<br /><br />";

    $data = file_get_contents($file);

    if (!xml_parse($parser, $data, true)) {
        print "<br /><br />Parse error!";
    } else {
        print "<br /><br />Parsing complete.";

In the code, first the XML file is opened for read access using fopen($file, "r"), then the contents of the file are read entirely into a string, data, using $data = fread($fp, filesize($fp)). Note that you will need to alter the $file variable to point to an XML file that is stored somewhere PHP can access it.

With the XML document read into a string, fclose() is called, passing in the file handle we had opened for reading - in other words, we close the file we opened. As the data is now stored entirely in the string $data, we can now parse the data using xml_parse(). xml_parse() takes three parameters: a reference to the XML parser being used, the XML to parse, and a third parameter "is this all the XML to come?" The third parameter is very useful in situations where you want to feed XML chunk-by-chunk into the parser. xml_parse() returns true if the data was parsed successfully, and false if it fails.

In the code above, if the result from xml_parse() is false - that is, if the XML failed to parse - We print out a congratulatory status message, otherwise we print out failure.

If you have made this far, you have all the necessary ingredients to create a full XML parsing script. Do not fret, though, because you have already seen all the necessary code, it is just a matter of Bringing Everything Together.


Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Bringing Everything Together >>

Previous chapter: Callback function implementation

Jump to:


Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.