Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

PHP XML SimpleXML


May 11, 2021 PHP


Table of contents


PHP SimpleXML

PHP can build and parse xml based on SimpleXML, and from the examples in this section, you'll see how PHP uses SimpleXML to generate and parse xml format data.

PHP SimpleXML handles the most common XML tasks, while the rest is handled by other extensions.


What is PHP SimpleXML?

SimpleXML is a new feature in PHP 5.

The SimpleXML extension provides an easy way to get the name and text of an XML element.

Compared to DOM or Expat parsers, SimpleXML can read text data from XML elements in just a few lines of code.

SimpleXML converts XML documents (or XML strings) into objects, such as:

  • The element is converted to a single property of the SimpleXMLElement object. When multiple elements exist at the same level, they are placed in an array.
  • The property is accessed by using an associated array, where the index corresponds to the property name.
  • The text inside the element is converted to a string. If an element has more than one text node, it is arranged in the order in which they were found.

SimpleXML is quick to use when performing basic tasks like:

  • Read/extract data from XML files/strings
  • Edit text nodes or properties

However, when working with advanced XML, such as namespaces, it is best to use an Expat parser or XML DOM.


Installation

Starting with PHP 5, SimpleXML functions are part of the PHP core. You can use these functions without installation.


PHP SimpleXML instance

Suppose we have the following XML file, "note .xml":

<?xml version="1.0" encoding="ISO-8859-1"?>
 <note>
 <to>Tove</to>
 <from>Jani</from>
 <heading>Reminder</heading>
 <body>Don't forget me this weekend!</body>
 </note> 

Now we want to output different information about the XML file above:

Instance 1

Output$xml keys and elements of the variable, which is the SimpleXMLElement object:

<?php
$xml=simplexml_load_file("note.xml");
print_r($xml);
?>

Run an instance . . .

The above code will output:

SimpleXMLElement Object ( [to] => Tove [from] => Jani [heading] => Reminder [body] => Don't forget me this weekend! )

Instance 2

Outputs data for each element in the XML file:

<?php
$xml=simplexml_load_file("note.xml");
echo $xml->to . "<br>";
echo $xml->from . "<br>";
echo $xml->heading . "<br>";
echo $xml->body;
?>

Run an instance . . .

The above code will output:

Tove
Jani
Reminder
Don't forget me this weekend!

Instance 3

Output the element name and data for each child node:

<?php
$xml=simplexml_load_file("note.xml");
echo $xml->getName() . "<br>";

foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br>";
}
?>

Run an instance . . .

The above code will output:

note
to: Tove
from: Jani
heading: Reminder
body: Don't forget me this weekend!


More information about PHP SimpleXML

To learn more about PHP SimpleXML functions, visit our PHP SimpleXML reference manual.

At the end of this chapter, let's take a look at the relationship between PHP and AJAX in the next chapter!