Problem with DOMDocument->saveXML()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
praveenpvs
Forum Newbie
Posts: 6
Joined: Fri Dec 10, 2010 8:32 am

Problem with DOMDocument->saveXML()

Post by praveenpvs »

Hi everyone,

Code: Select all

$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:" . "<br />";
echo $doc->saveXML() ;

$doc->save('test2.xml'); // save as file 


echo "Retrieving only the title part:" . "<br />";
echo $doc->saveXML($title);
In the above code echo

Code: Select all

$doc->saveXML(); 
suppose to print the XML packet on the console. But it is not happening.
My output on the console is:
Retrieving all the document:
Retrieving only the title part:
But

Code: Select all

$doc->save('test2.xml');
is working for me, i mean it is saving the XML packet in the test2.xml.
This is what it saved in the test2.xml
<?xml version="1.0"?>
<book>
<title>This is the title</title>
</book>
I have used this echo $doc->saveXML() ; in many codes, but nowhere it worked for me. I am sorry if i have done any silly mistake,I am new to PHP programming.
please do help me.
Thanks a lot for your help.

regards
Praveen.
praveenpvs
Forum Newbie
Posts: 6
Joined: Fri Dec 10, 2010 8:32 am

Re: Problem with DOMDocument->saveXML()

Post by praveenpvs »

Hi,

Can anyone help me out of this?
Still i am not able to figure out what is the problem with this code.
Thanks for your kind help.

regards
Praveen.
Post Reply