tags:

views:

137

answers:

2

The below code is fetched from php.net (http://docs.php.net/manual/en/domdocument.savexml.php). My problem is - it doesn't work. My only output from this is: "Saving all the document: Saving only the title part:". What am I missing here?

$doc = new DOMDocument('1.0'); // we want a nice output
$doc->formatOutput = true;
$root = $doc->createElement('book'); $root = $doc->appendChild($root);
$title = $doc->createElement('title');
$title = $root->appendChild($title); $text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text); echo "Saving all the document:\n";
echo $doc->saveXML() . "\n"; echo "Saving only the title part:\n";
echo $doc->saveXML($title);

A: 

There are tags in the output generated from saveXML(), and the browser tries to interpret those.

If you use the following portion of code :

$doc = new DOMDocument('1.0'); // we want a nice output
$doc->formatOutput = true;
$root = $doc->createElement('book');
$root = $doc->appendChild($root);
$title = $doc->createElement('title');
$title = $root->appendChild($title);
$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);
echo $doc->saveXML();

Then, the browser doesn't display a thing, as tags are interpreted.


But, if you replace the last one by this one :

echo '<pre>' .htmlspecialchars($doc->saveXML()) . '</pre>';


Then, you'll se the ouput :

<?xml version="1.0"?>
<book>
  <title>This is the title</title>
</book>

This is because the tags are escaped, now -- and, so, not interpreted.


Another solution would be to :

  • Keep the first portion of code
  • But use View Source in your browser, instead of just looking at the rendered page.
Pascal MARTIN
Why do people always give this answer when folks are not using the correct `Content-Type`. Rather than mangle the output, just kindly inform the browser what type of content it should be rendering.
salathe
But it _could_ be the answer in this case. maralbjo sends the complete document _plus_ a fragement. Ok, this might only be a test but it would invalidate the xml document anyway, so `Content-type: text/xml` alone won't fix the script...
VolkerK
@salathe : because, in this specific case, sending an XML Content-type would not work much better, as there is more than just XML sent to the output ;;; but, in a general situation, I agree that this *(sending the right Content-type)* would be much a better solution.
Pascal MARTIN
@both I never said to send it as `text/xml`... he wanted to see the text, so send it as plain text.
salathe
Anyway, thanks a whole lot for clearing up this. I literally lost a night of sleep to this, but now I got it. Thanks everyone.
maralbjo
A: 

PHP sends a Content-type http header. And by default it's text/html. I.e. the client is supposed to interpret the response document as html. But you're sending an xml document (and some text and another fragment, which invalidates the output).
If you want to send an xml document tell the client so, e.g. via header('Content-type: text/xml')

$doc = new DOMDocument('1.0');
$doc->formatOutput = true;

$root = $doc->appendChild($doc->createElement('book'));
$title = $root->appendChild($doc->createElement('title', 'This is the title'));

if (headers_sent() ) {
  echo 'oh oh, something wnet wrong';
}
else {
  header('Content-type: text/xml; charset=utf-8');
  echo $doc->saveXML();
}
VolkerK