Problems with DOM/XML functions

Discussing Html . Code , Software , other

Moderators: egami, macek, gesf

Post Reply
Misulache12
New php-forum User
New php-forum User
Posts: 8
Joined: Wed Jan 28, 2015 2:16 am

Mon Feb 09, 2015 1:08 am

Hello,
I don't succeed in creating an xml text using DOM/XML functions from PHP. If I run phpinfo() from browser, at DOM section, it tells me that DOM/XML is enabled but the following instructions don’t create the expected text on the screen:

<?php

$doc = new DOMDocument('1.0');
// we want a nice output
$dom = new DOMDocument('1.0', 'iso-8859-1');
echo $dom->saveXML(); /* <?xml version="1.0" encoding="iso-8859-1"?> */
$xhtml = (string) $doc->saveXML($doc->doctype);
$xhtml .= "\n";
$xhtml .= (string) $doc->saveXML($doc->documentElement);
echo $xhtml;

$doc->formatOutput = true;
$doc->encoding = "UTF-8";

$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));

?>
On the screen is displayed the text: "Saving all the document:Saving only the title part:". I conclude from this result that the DOM/XML functions don’t work and I assume that it must be an installation option witch I have not. I work on a computer with windows 7 and Internet Explorer with IIS activated.

Misulache12
New php-forum User
New php-forum User
Posts: 8
Joined: Wed Jan 28, 2015 2:16 am

Mon Feb 09, 2015 2:43 am

I think that I've worked to much on the previous php script and someone couldn't understand many thing from it. I hope that the following script is much clear. What should the browser display when it runs the script because to me it shows only the message: "Saving all the document. Saving only the titlepart." without even display the "\n" character.

<?php
header("Cache-control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
?>
<?php
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
//$xhtml = (string) $doc->saveXML($doc->doctype);
//$xhtml .= "\n";
//echo $doc->saveXML();

$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);
//$doc->save("http://localhost/prog_loturi/test.xml")
?>

Post Reply