How to Parse XML File Using PHP

Welcome back to today’s tutorial where we’ll pick up from where we left off at learning to use a REST client to access CCB data via the CCB API.

Before tackling today’s tutorial, I suggest you wisely review last week’s tutorial: How to Retrieve Data using CCB API and Rest Client.

Today’s tutorial’s builds on the previous tutorial by learning to parse the save XML file.

If you’ve been around CCBTutorials.com, then you may recall I shared a tutorial about parsing and saving XML as CSV/Excel file.

Although somewhat the same, today’s tutorial is easier because you’re not making a CCB API call since you’ve already done so and saved data to XML using the REST client.

The previous tutorial used CCB’s individual_profiles API service via the REST client. You should have saved the file as an XML file.

If you didn’t, then review the video below and save the file in your web directory, naming it response-data-export.xml.

Now that you have save your xml file, open the file via a web browser. It should look like the following image below (a bit longer):

As shown above, the web browser displays the XML data in a tree format that can be expanded or collapsed using using the gray arrows beside particular XML elements.

To parse the XML file, we’ll use a simple PHP file in conjunction with a built-in PHP method for parsing XML files: simplexml_load_file.

PHP’s simplexml_load_file is not to be confused with simplexml_load_string that we used to transform CCB XML to JSON in a previous tutorial.

The simplexml_load_string should be used when parsing an XML string and simplexml_load_file should be used when parsing an XML file.

To get started reading and parsing the xml file, open a text editor of your choice, saving and naming the file as follows: read-xml-individuals.php.

The first thing you’ll need to is to define a file variable and give it the value of the file name or path to filename.

Next create and define a xml variable, setting it equal to the simplexml_load_file method, passing to it the file variable as an argument.

I added the or and die statements as simple error checking should the file not be found, not be opened, or simply not able to create the XML object.

Also, be sure your XML and PHP files are in the same directory if you’re following this tutorial step-by-step.

Once the xml variable has been properly defined, I’ve created two more variables: allPersons and eachPerson.

If you’re familiar with previous tutorials, then you’re quite familiar with using PHP’s built-in XPATH query to parse a XML response for specific elements using an expression (e.g., //individuals/individual).

Because we’re using simplexml_load_file method, there’s not a need for XPATH query due to simply being able to already access the data elements using a reference or pointer, as it’s known in other languages (i.e. -> ).

As you see below, the allPersons variable is equal to the response->individuals XML data point. This variable is created to display the total count of individuals in your file, which we’ll soon get too.

The eachPerson variable is equal to the $allPersons->individual XML data point. Notice how we added or referenced the individual by adding to the allPersons variable.

To access the individuals XML element and attribute count, we’ll use PHP’s attributes method to return the total number of individuals captured within the XML file via the count variable.

Then, we’ll create another line of code to echo the first line of data to the web browser for total number of persons (shown below).

The final step of this tutorial is iterating over each of the XML individual elements using a foreach statement, passing to it the eachPerson variable as a person.

Within the foreach statement, I simply echo each individual’s full name by referencing the XML element node name full_name.

That’s it! This is how you parse a XML file that you made using a REST client or a XML in general.

Your PHP in its entirety should look like the following:

When you view your file via a web browser, it should display something like the following:

Although I chose to display only the full name of each person, you can decide to access and add as many data points as you wish by using the XML element node name or attribute name.

Well, that’s all for now. Drop me a line should you have questions or comments. See you back here next week! 😉


Please comment if this tutorial has helped you.