0

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
1
  • saveXML() returns a string you need to echo it or assign a variable. Commented Aug 9, 2019 at 15:47

1 Answer 1

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.