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

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
ThaJock
Forum Newbie
Posts: 11
Joined: Tue Nov 04, 2008 10:15 pm

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

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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

Post 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');
ThaJock
Forum Newbie
Posts: 11
Joined: Tue Nov 04, 2008 10:15 pm

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

Post 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.
Post Reply