Parsing XML in PHP

To parse XML in PHP, you can to use the SimpleXML extension. The SimpleXML is an extension allowing us to easily manipulate and retrieve data from XML strings. The SimpleXML turns XML into a data structure that can be iterated over like a collection of arrays and objects. As of PHP 5, the SimpleXML functions are part of the PHP core, and no installation is required to use them. In this PHP Parse XML example, we use the simplexml_load_string method, which provides a convenient way to access XML content. You can find more examples of parsing XML in PHP below. Click Execute to run the PHP Parse XML Example online and see the result.
Parsing XML in PHP Execute
<?php
$xmlString = '<Order>
  <Customer>Jack</Customer>
  <Rating>6.0</Rating>
</Order>';

$xml = simplexml_load_string($xmlString);

$customer = $xml->Customer;

echo "Customer: " . $customer;
?>
Updated: Viewed: 1704 times

What is PHP?

PHP is a general-purpose server-side scripting language primarily used in web development. It can be embedded in HTML and executed on the server side, allowing developers to create dynamic web content. PHP supports various programming paradigms, such as procedural, object-oriented, and functional programming. PHP is compatible with multiple operating systems, such as Linux, Windows, and MacOS, and can interact with numerous databases, including MySQL and PostgreSQL. PHP has an extensive global community that contributes to a large base of open-source projects, constantly enriching the PHP ecosystem.

What is XML?

XML, short for Extensible Markup Language, is a flexible text-based structure widely used in web development, network communications, and data storage. XML is primarily used to encode data in a human-readable and machine-readable format to ensure data compatibility between systems. XML's inherent flexibility and scalability have made it the accepted standard for many programming languages, including PHP, with extensive XML processing libraries. XML allows fine-grained structuring with elements, declarations, and processing instructions, and unlike JSON, XML supports comments.

XML Example
<Order>
  <Id>14</Id>
  <Customer>Jason</Customer>
  <Price>29.00</Price>
</Order>

PHP Parse XML Examples

The following are examples of XML parse in PHP:

Parse using SimpleXML functions

PHP provides a built-in SimpleXML extension that simplifies the parsing of XML documents. SimpleXML allows you to refer to an XML structure's elements as objects, making the code more readable and understandable. The following is an example of parsing XML using the SimpleXML functions:

PHP Parse with SimpleXML Example
<?php
$xmlString = '<Order>
  <Customer>Jack</Customer>
  <Rating>6.0</Rating>
  </Order>';

$xml = simplexml_load_string($xmlString);

$customer = $xml->Customer;
$rating = $xml->Rating;

echo "Customer: " . $customer;
echo "Rating: " . $rating;
?>

#output: Customer: JackRating: 6.0

Parsing XML using DOM extensions

PHP provides a way to parse XML using the DOM (Document Object Model) extension. The DOM represents an XML document as a tree structure of objects that can be easily manipulated. The following is an example of parsing XML using the DOM extensions:

PHP Parse with DOM Extensions Example
<?php
$xmlString = '<Order>
  <Customer>Jack</Customer>
  <Rating>6.0</Rating>
  </Order>';

$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xmlString);

$customer = $xmlDoc->getElementsByTagName('Customer')->item(0)->nodeValue;
$rating = $xmlDoc->getElementsByTagName('Rating')->item(0)->nodeValue;

echo "Customer: " . $customer;
?>

#output: Customer: Jack

Parsing XML using the SimpleXMLElement library

PHP has a SimpleXMLElement library that provides advanced functionality for working with XML documents. The following is an example of parsing XML using the SimpleXMLElement library:

PHP Parse with DOM Extensions Example
<?php
$xmlString = '<Order>
  <Customer>Jack</Customer>
  <Rating>6.0</Rating>
  </Order>';

$xml = new SimpleXMLElement($xmlString);

$customer = $xml->Customer;
$rating = $xml->Rating;

echo "Rating: " . $rating;
?>

#output: Rating: 6.0

Parsing XML using the XMLReader

XMLReader is a PHP extension designed for parsing large XML documents efficiently. XMLReader provides a streaming approach, ideal for processing huge XML files without excessive memory.

PHP Parse with XMLReader Example
<?php
$xmlString = '<Order>
  <Customer>Jack</Customer>
  <Rating>6.0</Rating>
  </Order>';

$xmlReader = new XMLReader();

libxml_disable_entity_loader(true);

$xmlReader->XML($xmlString);

$customer = '';

while ($xmlReader->read()) {
    if ($xmlReader->nodeType == XMLReader::ELEMENT && $xmlReader->name === 'Customer') {
        $xmlReader->read();
        $customer = $xmlReader->value;
        break; // Stop reading after finding the "Customer" element
    }
}

echo $customer;
?>

#output: Jack

See also