I want to create a XML File (1.0) via PHP but I have problems with using DOMDocument.
My code:
<?php
header('Content-Type:text/xml');
$xml = new DOMDocument('1.0', 'utf-8');
$root = $xml->createElement('root');
$root->setAttribute('id', '1234');
$root->setAttribute('date', '26.02.2018');
//info
$xml->appendChild($root);
$info = $xml->createElement('information');
$root->appendChild($info);
//updates
$updates = $xml->createElement('updates');
$info->appendChild($updates);
//updated
$updated= $xml->createElement('updated');
$updates->appendChild($updated);
//client
$client = $xml->createElement('client', 'exampleClient');
$updates->appendChild($updated);
//clientID
$clientID = $xml->createElement('clientID', '123456');
echo $xml->saveXML();
?>
Output:
<root id="1234" date="26.02.2018">
<information>
<updates>
<updated/>
</updates>
</information>
</root>
As you can see, it doesn't display the createElement() Data like 'client' and 'exampleClient'.
My desired output:
<root id="1234" date="26.02.2018">
<information>
<updates>
<updated>
<client>exampleClient</client>
<clientID>123456</clientID>
</updated>
</updates>
</information>
</root>
Does anyone have any idea how I can solve the problem?
new DOMDocumentand use namespaces instead of classnames likenew Vendor/DOMDocumentclientandclientIDelements but you didn't attach them to the XML document. Of course they don't show up in the output of$xml->saveXML().