What is "simplexml"
Simplexml is a PHP extension that allows you to read and manipulate XML in a very simple way, albeit with several restrictions. The main constraint is that an element (tag) can only contain text or other elements, never text together with other elements, as occurs in HTML.
Example of XML that is not correctly handled by Simplexml:
<?xml version="1.0" ?>
<teste>
<frase>A linguagem <negrito>PHP<negrito> é legal.</frase>
</teste>
Simplexml was made to manipulate XML that represent data packages on a given record, different from HTML, which is used to format a document, ie are distinct purposes.
The extension provides two main functions: simplexml_load_string and simplexml_load_file. The first loads an XML from a string and the second loads an XML from the name of a file. Both return an object of the Simplexmlelement class, which is the main class of the extension.
Leitura usando SimpleXML
In general, the Simplexmlelement class represents an element within XML, but can also represent a collection of elements. Assuming the following XML:
<?xml version="1.0" ?>
<livro>
<nome>Exemplos de XML</nome>
<data formato="w3c">2011-02-28</data>
<edicao>1</edicao>
<autor>
<nome>Rubens Takiguti Ribeiro</nome>
<site>http://rubsphp.blogspot.com</site>
</autor>
<comentarios>
<comentario>
<autor>Alguém</autor>
<texto>Gostei muito do livro</texto>
</comentario>
<comentario>
<autor>Outro</autor>
<texto>Poderia ter mais figuras.</texto>
</comentario>
</comentarios>
</livro>
To get the book and author data, just access them by the hierarchical structure of XML:
$xml = simplexml_load_file('exemplo.xml');
echo strval($xml->nome); // Obtem o nome do livro
echo strval($xml->data); // Obtem a data do livro
echo strval($xml->data['formato']); // Obtem o atributo "formato" da data do livro
echo strval($xml->autor->nome); // Obtem o nome do autor do livro
echo strval($xml->autor->site); // Obtem o endereco do site do autor do livro