How to Parse an XML Document Using PHP 


               




How to Parse an XML Document Using PHP

PHP



In this article, we show how to parse an XML document using PHP.

PHP is a powerful language that can extract the information you need from an XML document.

It's actually very simple.

Say, for example, there is an API written in XML and you want to extract data from it. You can do this with PHP.

In this example, we're going to extract information from a simple XML document that lists the contact information of 3 different people. Each person has a unique id. And for each person, we have their name, email, and phone number.

The XML document that we are going to parse can be found at the following link: contacts.xml.

So we'll show now how to use PHP to extract from the fields of the person element.

PHP Code

So we use the following PHP code to extract the name, email, and phone number of each person on the XML document.



The $xmlload variable is set equal to the simplexml_load_file() function. Inside of this function, we put the XML document that we want to parse. Since the XML document that I want to parse is contact.xml, we put this inside of this function.

The simplexml_load_file() function can take relative paths or absolute paths. So if the XML is located on an external site, which is going to be the case the majority of the times, you put the absolute pathway to the file inside of this function .

This function loads the XML document that you want to parse.

We then make a foreach loop. This loops through all the person elements in the XML document.

Inside of this foreach loop, we put the name of the element we want to find each of in the document. In this case, we want to loop through each person element in the XML document. We then use the as keyword to save each occurrence as the PHP variable $person.

We then create a PHP variable for all the elements within the person element. We create a variable for the name, email, phonenumber, city, and state elements. The $person element represents the person element we are accessing. We then use "->" and specify the element within this person element that we want the data of. This is how we extract all the data within the elements of the person element.

We then echo out the results.

So using the XML document above, the PHP output is shown below.

Actual PHP Output


name: John Smith
email: Johnsmith@gmail.com
phone number: (111)111-1111
city: Miami
state: Florida

name: Peter Phils
email: Peterphils@gmail.com
phone number: (222)222-2222
city: New York City
state: New York

name: Michael Thoms
email: Michaelthoms@gmail.com
phone number: (333)333-3333
city: Daytona Beach
state: Florida



Parsing for More Specific Data

Let's say you don't want to output every person. You instead want to find every person that lives in Florida and only output their information.

The following PHP code does this below.



So now we've shown how to parse data from an XML document and only extract data that matches a certain criterion.

Now we only show people who live in Florida from an XML document.

We loop through all the person elements and use a simple if statement to see if the state equals Florida.

The PHP output of the above code is shown below.

Actual PHP Output


name: John Smith
email: Johnsmith@gmail.com
phone number: (111)111-1111
city: Miami
state: Florida

name: Michael Thoms
email: Michaelthoms@gmail.com
phone number: (333)333-3333
city: Daytona Beach
state: Florida



How to Parse an Attribute of an Element

Now that you know how to parse elements of an XML document using PHP, we now show how to parse an attribute of element.

Going back to our example XML document, the only attribute we use is the id attribute. This ranges from 1 to 3 for the 3 individual people.

To parse an attribute of an element, the PHP code to do is shown below.

Instead of using "->" like when accessing an element, to access an attribute, [] is used.

So the full PHP code to show all person elements along with the ID attributes is shown below.



Now all the id attributes will be printed as well.

Actual PHP Output


name: John Smith
id: 1
email: Johnsmith@gmail.com
phone number: (111)111-1111
city: Miami
state: Florida

name: Peter Phils
id: 2
email: Peterphils@gmail.com
phone number: (222)222-2222
city: New York City
state: New York

name: Michael Thoms
id: 3
email: Michaelthoms@gmail.com
phone number: (333)333-3333
city: Daytona Beach
state: Florida




So this is all the basics of parsing a basic XML document using PHP.

This is done through SimpleXML, which makes it easy for PHP to interact with XML. This can be used in all types of applications, such as when you want to read a configuration file written in XML or parse an RSS feed.


HTML Comment Box is loading comments...