The following code produces the XML file that I am after.

$dom = new DOMDocument('1.0', 'UTF-8');
// pretty formatting
$dom->formatOutput = true;

// create root element
$root = $dom->createElement('Poem');
$root->setAttribute('object_id', '542');
$dom->appendChild($root);

// create title element
$title = $dom->createElement('title');
$root->appendChild($title);

// create a node for the title element
$text = $dom->createTextNode('The Unknown Poem');
$title->appendChild($text);

// save xml as string
$test = $dom->saveXML();
echo($test);
$dom->save('testDOM.xml');

I get testDom.xml contaning the details that I need. However, the echo($test); prints nothing.
What might be the cause I wonder? I tried print_r() too. Any suggestions?

Additional Information

XAMPP (on Win 8) is running fine. Apache is running.
I can echo 'other string values';.

Member Avatar for diafol

Not really looked hard. Which browser are you using? SOme browser do not display XML at all - have a look in page/view source of the browser to see if it's there.

I'm sorry, I didn't look hard enough.
I've just tested itwith IE, Fire Fox and Chrome. I see nothing on screen. Page view/source option with these browsers did not reveal the output of echo XML. But, I ended up with the correct XML file on server.

I can open and see the content of the XML file correctly on IE, Fire Fox and Chrome.

If I execute this code in http://sandbox.onlinephpfunctions.com/ it prints echo correctly.
If I execute this code in http://phptester.net/, it does not print echo output.

Hence, I'm puzzled.

On line 20, I did the following and can see the echo'd code just fine.

echo(htmlspecialchars($test));

Member Avatar for diafol

On line 20, I did the following and can see the echo'd code just fine.

If that's the case, then it's as I originaly thought.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.