Saving a DOM tree into a string
Posted: Sun Sep 12, 2010 9:13 pm
Hi, I am a newbie in PHP. Now I am learning some sample using XML/DOM.
I just cut and paste the code of Example #1 Saving a DOM tree into a string
from this url: http://www.php.net/manual/en/domdocument.savexml.php
When I open the code in Chrome browser, I did not get the expected output.
My output was :
Saving all the document: Saving only the title part:
The expected output was:
Saving all the document:
<?xml version="1.0"?>
<book>
<title>This is the title</title>
</book>
Saving only the title part:
<title>This is the title</title>
Question:Could you please help to explain why I did not get the expected output?
Thanks.
The following is the code from the mentioned url:
<?php
$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 "Retrieving all the document:\n";
echo $doc->saveXML() . "\n";
echo "Retrieving only the title part:\n";
echo $doc->saveXML($title);
?>
I just cut and paste the code of Example #1 Saving a DOM tree into a string
from this url: http://www.php.net/manual/en/domdocument.savexml.php
When I open the code in Chrome browser, I did not get the expected output.
My output was :
Saving all the document: Saving only the title part:
The expected output was:
Saving all the document:
<?xml version="1.0"?>
<book>
<title>This is the title</title>
</book>
Saving only the title part:
<title>This is the title</title>
Question:Could you please help to explain why I did not get the expected output?
Thanks.
The following is the code from the mentioned url:
<?php
$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 "Retrieving all the document:\n";
echo $doc->saveXML() . "\n";
echo "Retrieving only the title part:\n";
echo $doc->saveXML($title);
?>