Page 1 of 1

DOMDocumentFragment->appendXML('php code here');

Posted: Sun Mar 21, 2010 2:55 pm
by ThaJock
I'm trying to output html with DOM and everything is working fine but now I need to also add some php code in the document.
I read that using DOMDocumentFragment will do the job but it does nothing my case. For example:

<?php
$dom = new DOMDocument('1.0');
$body = $dom->createElement("body");
$dom->appendChild($body);
$php = $dom->createDocumentFragment();
$php->appendXML('<?php echo "good"; ?>');
$body->appendChild($php);
echo $dom->saveXML();
?>

When I launch this in my browser the page is just blank. In the source code you can see the php code (which isn't desired) but it doesn't execute.

Any help would be greatly appreciated.

Re: DOMDocumentFragment->appendXML('php code here');

Posted: Sun Mar 21, 2010 3:04 pm
by John Cartwright
Your already within PHP tags, so you don't need to embed more tags to execute PHP. You just pass the string or variable or the function, i.e.,

Code: Select all

$php->appendXML('good');

Re: DOMDocumentFragment->appendXML('php code here');

Posted: Sun Mar 21, 2010 4:16 pm
by ThaJock
I understand that but I'm trying to get DOM to read the php from an file being loaded into the document.

Code: Select all

 
$php = new DOMDocument('1.0');
$html = new DOMDocument('1.0');
 
$php->loadHTMLFile('file.php');
 
$body = $html->importNode($php->getElementsByTagName("body")->item(0);
 
$import = $html->importNode($body,true);
$html->appendChild($import);
 
echo $html->saveXML();
 
The file that I am loading has some php in it but it is being ignored.