I'm struggling with SimpleXMLElement for quite some time and I can't figure out how to output a well formatted XML document.
With a SQL query, I retrieve some data as XML format.
select S_MODELE, S_ID from stock FOR XML RAW ('Product'), ROOT ('Products'), ELEMENTS"
In php, I have a variable called $values
that is equal to this :
<products>
<product>
<S_MODELE>Some text</S_MODELE>
<S_ID>1</S_ID>
</product>
<product>
<S_MODELE>Some text</S_MODELE>
<S_ID>2</S_ID>
</product>
<product>
<S_MODELE>Some text</S_MODELE>
<S_ID>3</S_ID>
</product>
</products>
I'm then trying to output a properly formatted XML file in the browser :
header('Content-Type: text/xml');
$xml = new SimpleXMLElement($values);
echo $xml->asXML();
And I get something like this :
Thank you for your time.
The screenshot does not match the output example. It looks like you have multiple XML documents as text inside another XML structure.
Be aware that if you look at an XML as HTML in the browser you will see only the text content. In this case the inner XML documents.
If $values
already contains the XML document, you can just echo $values
. Here is no need to load it into a DOM or SimpleXMLElement, just to serialize it again.