Read XML files in PHP

March 29, 2014 | Last tested: May 2015 | 1380 views | Comments

Parse XML files using simplexml_load_file() in php.

For example.

The XML file used on this example was from the generated XML file named members.xml from the tutorial Create an XML file with PHP.

Read an existing XML file, and print it's contents on the page.

$xml = simplexml_load_file('members.xml');

print_r($xml);

//result
/*
SimpleXMLElement Object ( [member] => Array ( [0] => SimpleXMLElement Object ( [lastName] => Jackson [firstName] => Barbara [age] => 27 [sex] => F [location] => Florida ) [1] => SimpleXMLElement Object ( [lastName] => Kimball [firstName] => Andrew [age] => 25 [sex] => M [location] => Texas ) [2] => SimpleXMLElement Object ( [lastName] => Baker [firstName] => John [age] => 28 [sex] => M [location] => Arkansas ) [3] => SimpleXMLElement Object ( [lastName] => Gamble [firstName] => Edward [age] => 29 [sex] => M [location] => Virginia ) [4] => SimpleXMLElement Object ( [lastName] => Anderson [firstName] => Kimberly [age] => 23 [sex] => F [location] => Tennessee ) [5] => SimpleXMLElement Object ( [lastName] => Houston [firstName] => Franchine [age] => 25 [sex] => F [location] => Idaho ) [6] => SimpleXMLElement Object ( [lastName] => Franklin [firstName] => Howard [age] => 24 [sex] => M [location] => California ) [7] => SimpleXMLElement Object ( [lastName] => Chen [firstName] => Dan [age] => 26 [sex] => M [location] => Washington ) [8] => SimpleXMLElement Object ( [lastName] => Daniel [firstName] => Carolyn [age] => 27 [sex] => F [location] => North Carolina ) [9] => SimpleXMLElement Object ( [lastName] => Englert [firstName] => Grant [age] => 25 [sex] => M [location] => Delaware ) ) )
*/

 

To make it easier to read, and debug, a header content type can be added.

$xml = simplexml_load_file('members.xml');

header('Content-Type: application/json');
print_r($xml);

//result
/*
SimpleXMLElement Object
(
    [member] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [lastName] => Jackson
                    [firstName] => Barbara
                    [age] => 27
                    [sex] => F
                    [location] => Florida
                )

            [1] => SimpleXMLElement Object
                (
                    [lastName] => Kimball
                    [firstName] => Andrew
                    [age] => 25
                    [sex] => M
                    [location] => Texas
                )

            [2] => SimpleXMLElement Object
                (
                    [lastName] => Baker
                    [firstName] => John
                    [age] => 28
                    [sex] => M
                    [location] => Arkansas
                )

            [3] => SimpleXMLElement Object
                (
                    [lastName] => Gamble
                    [firstName] => Edward
                    [age] => 29
                    [sex] => M
                    [location] => Virginia
                )

            [4] => SimpleXMLElement Object
                (
                    [lastName] => Anderson
                    [firstName] => Kimberly
                    [age] => 23
                    [sex] => F
                    [location] => Tennessee
                )

            [5] => SimpleXMLElement Object
                (
                    [lastName] => Houston
                    [firstName] => Franchine
                    [age] => 25
                    [sex] => F
                    [location] => Idaho
                )

            [6] => SimpleXMLElement Object
                (
                    [lastName] => Franklin
                    [firstName] => Howard
                    [age] => 24
                    [sex] => M
                    [location] => California
                )

            [7] => SimpleXMLElement Object
                (
                    [lastName] => Chen
                    [firstName] => Dan
                    [age] => 26
                    [sex] => M
                    [location] => Washington
                )

            [8] => SimpleXMLElement Object
                (
                    [lastName] => Daniel
                    [firstName] => Carolyn
                    [age] => 27
                    [sex] => F
                    [location] => North Carolina
                )

            [9] => SimpleXMLElement Object
                (
                    [lastName] => Englert
                    [firstName] => Grant
                    [age] => 25
                    [sex] => M
                    [location] => Delaware
                )

        )

)
*/

 

Loop through the data, and display each record..

$xml = simplexml_load_file('members.xml');

foreach($xml AS $member){
	foreach($member AS $memberDetail){
		echo $memberDetail;
		echo '<br>';
	}
	echo '<br>';
}

//result
/*
Jackson
Barbara
27
F
Florida

Kimball
Andrew
25
M
Texas

Baker
John
28
M
Arkansas

Gamble
Edward
29
M
Virginia

Anderson
Kimberly
23
F
Tennessee

Houston
Franchine
25
F
Idaho

Franklin
Howard
24
M
California

Chen
Dan
26
M
Washington

Daniel
Carolyn
27
F
North Carolina

Englert
Grant
25
M
Delaware
*/

 

Get the lastName of the first member.

$xml = simplexml_load_file('members.xml');

echo $xml->member[0]->lastName;

//result
//Jackson

 

Get the age of the seventh member.

$xml = simplexml_load_file('members.xml');

echo $xml->member[6]->age;

//result
//24

 

Get the location and gender of the last member.

$xml = simplexml_load_file('members.xml');

echo $xml->member[count($xml->member)-1]->sex; //sex
echo '<br>';
echo $xml->member[count($xml->member)-1]->location; //location

//result
//M //sex
//Delaware //location