Page 1 of 1

PHP's DOM making some trouble

Posted: Thu Jun 15, 2006 9:49 am
by webmaster333
Hi,

I have two XML files. The first contains information about a menu on the page. The second one contains general information about the page. What I wanna do is to merging these two XML documents.
I'll explain my troubles later and will first post the two XML files and my PHP code.

Menu file

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<navigo>
	<topMenu>
		<tab url="#" itemId="1">Startseite</tab>
		<tab url="#" itemId="2">System</tab>
		<tab url="#" itemId="3">Foundation</tab>
		<tab url="#" itemId="4" status="active" permission="1">Intern</tab>
		<tab url="#" itemId="5" permission="1">Admin-Center</tab>
	</topMenu>
</navigo>
General information file

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<navigo>
	<user>
		<loggedIn>0</loggedIn>
		<userName>Georg Dresler</userName>
		<userId>0</userId>
		<userGroup>0</userGroup>
		<userGroupId>0</userGroupId>
	</user>
	<page>
		<title>Navigo eV</title>
		<metaTags>
			<set1>test</set1>
		</metaTags>
		<scripts>
			<js>navigo-lib/main.js</js>
			<js>navigo-lib/prototype.js</js>
			<js>navigo-lib/md5.js</js>
		</scripts>
		<style>
			<css>navigo-lib/main.css</css>
			<css option="lt IE 7">navigo-lib/mainIE6.css</css>
		</style>
		<inPlaceEditing>0</inPlaceEditing>
		<inPlaceEditingAct>0</inPlaceEditingAct>
	</page>
	<topMenuPlaceHolder>topMenu</topMenuPlaceHolder>
</navigo>
PHP code

Code: Select all

<?php
	// load topMenu and save as documentFragment
	$XML = new DOMDocument();
	$XML->load("navigo-xml/tpl_topMenu.xml");
	$XMLFragment =$XML->createDocumentFragment();
	$XMLNode =$XML->getElementsByTagName("topMenu")->item(0);
	$XMLFragment->appendChild($XMLNode); // contains complete topMenu
	
	// load mainDesign and import topMenu
	$XML->load("navigo-xml/tpl_mainDesign.xml");
	$XMLPlaceHolder =$XML->getElementsByTagName("topMenuPlaceHolder")->item(0);
	$XMLNode =$XML->importNode($XMLFragment, true);
	$XML->replaceChild($XMLNode, $XMLPlaceHolder);
		
	header("Content-Type: text/xml");

	echo $XML->saveXML();
?>
Everything works fine but including the menu into the second file. I want to replace the place holder with the menu node. But PHP always throws this error

Code: Select all

Fatal error: Uncaught exception 'DOMException' with message 'Not Found Error' in f:\Navigo\index.php:19 Stack trace: #0 f:\Navigo\index.php(19): DOMNode->replaceChild(Object(DOMDocumentFragment), Object(DOMElement)) #1 {main} thrown in f:\Navigo\index.php on line 19
PHP claims that the place holder node doesn't exist but this can be true as it DOES exist. The problem must be somewhere else but I can't figure out where.
Dunno if this helps but im using PHP 5.0.5

Please, anybody, help me! I've tried everything I can think about but it won't work.

cu
webmaster333

Posted: Thu Jun 15, 2006 1:34 pm
by Ambush Commander
XML is a DocumentNode, this appears to be interfering with the node replacement (didn't really expect that). Call the replace on the top-level node navigo, like this:

Code: Select all

$XML->firstChild->replaceChild($XMLNode, $XMLPlaceHolder);
Also, there is no need to create a document fragment. Just import the node itself.

Posted: Fri Jun 16, 2006 2:54 am
by webmaster333
Thanks for your help. It works perfect.
I also dropped the stuff wit the DocumentFragment. This makes the code even better :)