PHP:
Working with XML

How to:

Reading XML with SimpleXML:

$xmlString = '<?xml version="1.0" encoding="UTF-8"?>
              <note>
                <to>Tove</to>
                <from>Jani</from>
                <heading>Reminder</heading>
                <body>Don't forget this</body>
              </note>';
              
$xml = simplexml_load_string($xmlString);

echo $xml->to;       // Outputs: Tove
echo $xml->from;     // Outputs: Jani
echo $xml->heading;  // Outputs: Reminder
echo $xml->body;     // Outputs: Don't forget this

Writing XML with DOMDocument:

$dom = new DOMDocument('1.0', 'UTF-8');

$root = $dom->createElement('note');
$dom->appendChild($root);

$to = $dom->createElement('to', 'Tove');
$from = $dom->createElement('from', 'Jani');
$heading = $dom->createElement('heading', 'Reminder');
$body = $dom->createElement('body', 'Don't forget this');

$root->appendChild($to);
$root->appendChild($from);
$root->appendChild($heading);
$root->appendChild($body);

echo $dom->saveXML();

Sample output:

<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget this</body>
</note>

Deep Dive

XML, or eXtensible Markup Language, has been a staple in data serialization since its W3C recommendation in 1998. It’s verbose, human-readable, and strict in syntax, making it a reliable choice for configuration files, data interchange, and more. However, it’s been partially overshadowed by JSON for web APIs due to its simplicity and light-weight nature.

Programmers often pick XML when they need document validation provided by XML Schemas or when working within ecosystems that already heavily rely on it (like Microsoft Office file formats). Handling XML in PHP is straightforward with the SimpleXML extension for basic operations. For more complex manipulation, DOMDocument provides a robust set of features that allow for greater control, such as namespace handling and schema validation.

See Also