The simplexml_load_file() function converts the specified XML file into a SimpleXMLElement object.
PHP simplexml_load_file() Function has the following syntax.
simplexml_load_file(file,classname,options,ns,is_prefix);
Parameter | Is Required | Description |
---|---|---|
file | Required. | Path to the XML file |
classname | Optional. | Class of the new object |
options | Optional. | Additional Libxml parameters. Is set by specifying the option and 1 or 0 (TRUE or FALSE, e.g. LIBXML_NOBLANKS(1)) |
ns | Optional. | A namespace prefix or URI |
is_prefix | Optional. | A Boolean value. TRUE if ns is a prefix. FALSE if ns is a URI. Default is FALSE |
Possible values:
Returns a SimpleXMLElement object on success. FALSE on failure.
Convert an XML file into a SimpleXMLElement object, then output keys and elements of the object.
Assume we have the following XML file, "note.xml":
<?xml version="1.0" encoding="ISO-8859-1"?> <book> <name>PHP</name> <name>Java</name> </book>
PHP code
<?php
$xml=simplexml_load_file("test.xml");
print_r($xml);
?>
Output the data from each element in the XML file:
<?php
$xml=simplexml_load_file("test.xml");
echo $xml->to . "\n";
echo $xml->from . "\n";
echo $xml->heading . "\n";
echo $xml->body;
?>
Output the element's name and data for each child node in the XML file:
<?php
$xml=simplexml_load_file("test.xml");
echo $xml->getName() . "\n";
foreach($xml->children() as $child){
echo $child->getName() . ": " . $child . "\n";
}
?>
The following code shows how to get attribute and element as array.
<?php
$xml = simplexml_load_file('test.xml');
echo $xml->book[3]->title . ' (ISBN: ' . $xml->book[3]['isbn13'] . ')';
?>
The following code is for test.xml.
<?xml version='1.0' encoding='utf-8'?> <inventory> <book isbn13='1'> <title>PHP</title> <author>Jack</author> <publisher>Publisher 1</publisher> <description>PHP Book</description> </book> <book isbn13='2'> <title>XML</title> <author>Jane</author> <publisher>Publisher 2</publisher> <description>XML Book</description> </book> </inventory>
The code above generates the following result.
The following code shows how to check if an element is an array.
/* ww w . j a va 2 s.c om*/
<?php
$xml = simplexml_load_file('test.xml');
?>
<html>
<head>
</head>
<body>
<?php
foreach ($xml->book as $book) {
echo '<h2>' . $book->title . '</h2>';
echo '<p class="author">';
if (is_array($book->author)) {
echo implode(', ', $book->author);
} else {
echo $book->author;
}
echo '</p>';
echo '<p class="publisher">' . $book->publisher . '</p>';
echo '<p class="publisher">ISBN: ' . $book['isbn13'] . '</p>';
echo '<p>' . $book->description . '</p>';
}
?>
</body>
</html>
The following code is for test.xml.
<?xml version='1.0' encoding='utf-8'?> <inventory> <book isbn13='1'> <title>PHP</title> <author>Jack</author> <publisher>Publisher 1</publisher> <description>PHP Book</description> </book> <book isbn13='2'> <title>XML</title> <author>Jane</author> <publisher>Publisher 2</publisher> <description>XML Book</description> </book> </inventory>
The code above generates the following result.