I am creating an XML file in PHP like this...
$myXML = new DOMDocument();
$myXML ->formatOutput = true;
$data = $myXML ->createElement('data');
$data->nodeValue = 'mydata';
$final->appendChild($data);
$myXML ->save('/mypath/myfile.xml');
This works, but how can I convert this to use saveXML() instead? I have tried like this but I get nothing
$myXML->saveXML();
Where am I going wrong?
asked Aug 9, 2019 at 15:42
-
saveXML() returns a string you need to echo it or assign a variable.Jason K– Jason K2019年08月09日 15:47:13 +00:00Commented Aug 9, 2019 at 15:47
1 Answer 1
I see two things:
$final
is not declared. Change it.- In case saveXML() is called, the output has to be assigned to a variable or printed
Here goes the working code:
<?php
$myXML = new DOMDocument();
$myXML ->formatOutput = true;
$data = $myXML ->createElement('data');
$data->nodeValue = 'mydata';
$myXML->appendChild($data);
echo $myXML ->saveXML();
?>
Output:
<?xml version="1.0"?>
<data>mydata</data>
answered Aug 9, 2019 at 15:58
Comments
lang-php