Example 1 — Create an Object from XML
Pass a tiny XML string to new SimpleXMLElement and read the title text and the root name.
<?php
$xml = '<book><title id="1">PHP</title></book>';
$book = new SimpleXMLElement($xml);
echo $book->getName(), PHP_EOL;
echo (string) $book->title, PHP_EOL; How It Works
- The XML string holds one book element with a title inside.
- new SimpleXMLElement($xml) runs the constructor and parses the text.
- getName() returns the name of the root element, which is book.
- The title is cast to a string so echo prints the text PHP.

