fightstarr20
fightstarr20

Reputation: 12568

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?

Upvotes: 0

Views: 278

Answers (1)

fiveobjects
fiveobjects

Reputation: 4249

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>

Upvotes: 1

Related Questions