简体   繁体   中英

PHP Domdocument use saveXML instead of save

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM