← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 4 of 24

PHP XML Handling

XML is another way of writing information with labeled tags, like putting every item in a box with a name on it. PHP can read these boxes and pull out what you need.
Syntax
php
$xml = simplexml_load_string($xml_string);
$xml = simplexml_load_file("file.xml");

echo $xml->element_name;   // read an element
$xml->element_name = "new value";

What is XML?

XML represents structured data as nested tags, similar in spirit to HTML but designed for data exchange rather than display; PHP's SimpleXML extension turns an XML document into an easy-to-navigate object tree.

Example: What is XML?

php
<?php
// Declare `$xml`, set to `simplexml_load_string("<book><title>PHP Basics</title></book>")`
$xml = simplexml_load_string("<book><title>PHP Basics</title></book>");
// Print `get_class($xml)` to the output
echo get_class($xml);
?>

Reading XML Elements

Once loaded with simplexml_load_string() or simplexml_load_file(), XML elements become accessible as regular object properties, so <book><title>...</title></book> becomes $book->title without manual parsing.

Example: Reading XML Elements

php
<?php
// Declare `$xml`, set to `simplexml_load_string("<book><title>PHP Basics</title></book>")`
$xml = simplexml_load_string("<book><title>PHP Basics</title></book>");
// Print `$xml->title` to the output
echo $xml->title;
?>

Modifying XML Elements

Because SimpleXML elements behave like live references into the document, assigning a new value to $element->title actually updates the underlying XML structure, ready to be saved back out.

Example: Modifying XML Elements

php
<?php
// Declare `$xml`, set to `simplexml_load_string("<book><title>Old Title</title></book>")`
$xml = simplexml_load_string("<book><title>Old Title</title></book>");
$xml->title = "New Title";
// Print `$xml->asXML()` to the output
echo $xml->asXML();
?>

Creating XML with SimpleXML

addChild() and addAttribute() let you build XML documents programmatically from scratch, appending new nested elements and attributes onto an existing SimpleXMLElement.

Example: Creating XML with SimpleXML

php
<?php
// Create a new `SimpleXMLElement` instance with "<book></book>", stored in `$xml`
$xml = new SimpleXMLElement("<book></book>");
$xml->addChild("title", "PHP Basics");
$xml->addAttribute("id", "1");
// Print `$xml->asXML()` to the output
echo $xml->asXML();
?>

Looping Through XML Lists

When an XML document contains repeated elements (like multiple <item> tags), a standard foreach loop iterates over them cleanly, letting you process each one exactly like looping over an array.

Example: Looping Through XML Lists

php
<?php
// Declare `$xml`, set to `simplexml_load_string("<items><item>A</item><item>B</item></items>")`
$xml = simplexml_load_string("<items><item>A</item><item>B</item></items>");
// Loop over `$xml->item`, binding each item to `$item`
foreach ($xml->item as $item) {
    // Print `$item . "\n"` to the output
    echo $item . "\n";
}
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Not checking that simplexml_load_string returned an object, so invalid XML gives a confusing error.
  2. Reading an attribute with -> instead of the array syntax [attr].
  3. Not escaping special characters like & when building XML by hand.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.