Home > Article > Backend Development > PHP SimpleXML_PHP Tutorial
SimpleXML can read text data from elements in just a few lines of code compared to DOM or Expat parsers.
SimpleXML can convert XML documents into objects , such as:
Element - a single property converted to a SimpleXMLElement object. When there are multiple elements at the same level, they are placed in an array.
Properties - accessed using an associative array, where the subscripts correspond to the property names.
Element Data - Text data from the element is converted to a string. If an element has multiple text nodes, they are arranged in the order in which they are found.
SimpleXML is very fast to use when performing basic tasks like:
Read XML file
Extract data from XML string
Edit text nodes or attributes
However, when dealing with advanced XML, such as namespaces, it is better to use the Expat parser or the XML DOM.
SimpleXML functions are part of the core of PHP. No installation is required to use these functions.
XML file:
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
<?php $xml = simplexml_load_file("test.xml"); echo $xml->getName() . "<br />"; foreach($xml->children() as $child) { echo $child->getName() . ": " . $child . "<br />"; } ?>