Jump to content


Photo

Dom trouble


  • Please log in to reply
3 replies to this topic

#1 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 21 August 2006 - 06:31 PM

Ok, so I've overcome most of the issues I've faced sofar with using the DOM extension as an, what I call, 'indexed output buffer', but keep running into more... :(

Right now I have a problem with a node not being returned (NULL instead).
This is my "DomXml::appendElement". I use it a lot, and hasn't failed me before...

<?php
public function appendElement($parentElement,$tag,$attrArr=array(),$text=false){
	if(!is_object($parentElement)){
		throw new Exception('parentElement not an Object.');
	}
	if($text) {
		$element = $this->domDoc->createElement($tag,$text);
	}
		else {
		$element = $this->domDoc->createElement($tag);
	}
	if(!$parentElement->appendChild($element)){
		throw new Exception('Appending of element failed.');
	}
	if(!empty($attrArr) && count($attrArr)) {
		foreach($attrArr as $attrName=>$attrValue){
			$element->setAttribute($attrName,$attrValue);
		}
	}
	return $element;
}
?>

Now it does:

Fatal error: Uncaught exception 'Exception' with message 'parentElement not an Object.' in E:\John\Sites\CurrentDev\johnkleijn.nl\lib\domxml.class.php:88 Stack trace: #0 E:\John\Sites\CurrentDev\johnkleijn.nl\lib\page.class.php(58): DomXml->appendElement(NULL, 'div', Array, false) #1 E:\John\Sites\CurrentDev\johnkleijn.nl\lib\menu.class.php(59): Page->appendElement(NULL, 'div', Array) #2 E:\John\Sites\CurrentDev\johnkleijn.nl\index2.php(16): Menu->appendMenu('offerte', 'onlineofferte') #3 {main} thrown in E:\John\Sites\CurrentDev\johnkleijn.nl\lib\domxml.class.php on line 88


This leads me to this code (in Menu::appendMenu()):
<?php
if($name !== 'mainmenu') {	
			$parentNode = $backbone->getElementById('subnavcontainer');
			$sc = $backbone->appendElement($parentNode,'div',array('id'=>'selectedcontainer'));
			$usc = $backbone->appendElement($parentNode,'div',array('id'=>'unselectedcontainer'));
			foreach($items as $item) {
				if($item == $selectedItem) {
					echo 'sc: '.$sc;
					$layer = $backbone->appendElement($sc,'div',array('id'=>$item));
					$backbone->appendElement($layer,'img',
						array('src'=>'img.php?f=menu/'.$subdir.'/'.$item.'.gif','alt'=>''));
					$backbone->appendElement($layer,'div',array('id'=>$item.'line','class'=>'subnavselectedline'));
				}
					else {
					$layer = $backbone->appendElement($usc,'div',array('id'=>$item,'class'=>'subnav'));
					$backbone->appendElement($layer,'img',
						array('src'=>'img.php?f=menu/'.$subdir.'/'.$item.'.gif','alt'=>''));
					$backbone->appendElement($layer,'div',array('id'=>$item.'line','class'=>'subnavline'));
				}
			}
}
?>

echo 'sc: '.$sc; echos just 'sc: ', as $sc is appearantly NULL... $backbone is a global instance of the Backbone class, one of DomXml descendants.

Does ANYONE have ANY idea why DomXml::appendElement() returns NULL instead of the element appended???

#2 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 22 August 2006 - 07:34 AM

No replies AGAIN???

Geez.

I still haven't solved this one.

Maybe you could help me disect the track trace?

#0 E:\John\Sites\CurrentDev\johnkleijn.nl\lib\page.class.php(58): DomXml->appendElement(NULL, 'div', Array, false)
#1 E:\John\Sites\CurrentDev\johnkleijn.nl\lib\menu.class.php(59): Page->appendElement(NULL, 'div', Array)
#2 E:\John\Sites\CurrentDev\johnkleijn.nl\index2.php(16): Menu->appendMenu('offerte', 'onlineofferte')

menu.class.php(59): Page->appendElement(NULL, 'div', Array)

Is where thing first start going wrong. NULL should be an object ($sc).

That's this line:

$layer = $backbone->appendElement($sc,'div',array('id'=>$item));

This is where $sc = assigned:

$parentNode = $backbone->getElementById('subnavcontainer');
$sc = $backbone->appendElement($parentNode,'div',array('id'=>'selectedcontainer'));
echo 'parentnode: '.$parentNode->getAttribute('id');

The debug line  echoes "parentnode: subnavcontainer", like it should. So getElementById is working properly. But I don't see any reason in DomXml::appendElement why $element should return NULL... ???

DOMDocument->createElement normaly returns the element created...

#3 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 23 August 2006 - 08:10 AM

Still haven't found it. I'll follow the code path, including anything that might be relevant.

index2.php
<?php
global $backbone; //'global' keyword not really ness, but hey..
$backbone = new Backbone('transitional','iso-8859-1');
$backbone->addHeader('title','','domcontroltest');
$backbone->addCss('css.php',array('defaults','level1','level2','menu','subnav','login','textbox'));
$backbone->addJavascript('js.php',array('menu','XmlHttpRequest'));
$body = $backbone->body();
$backbone->appendChunk($body,file_get_contents('indexHTMLFrame.html'));

//Menu
$menu = new Menu('menu.xml');
$menu->appendMenu('mainmenu','offerte');
?>
DomXml::__construct() [Backbone::Page::DomXhtml::DomXml->__construct()]
<?php
public function __construct($rootName,$publicId,$systemId,$charset='utf-8')	{	
	$this->publicId = $publicId;
	$this->systemId = $systemId;
	$this->domDoc = self::createDocument($rootName,$this->publicId,$this->systemId);
	//DomDocument settings:
	$this->domDoc->preserveWhitespace = true;	
	$this->domDoc->formatOutput = true;
	$this->domDoc->encoding = $charset;
	//Relayed properties:
	$this->documentElement = $this->domDoc->documentElement;
}
?>
DomXml::appendChunk() [Backbone::Page::DomXhtml::DomXml->appendChunk()]
<?php
public function appendChunk($parentNode, $chunk)
{	
	@$frag = $this->domDoc->createDocumentFragment();
	@$frag->appendXML($chunk);
	if(!@$parentNode->appendChild($frag)) {
		$chunkDoc = self::createDocument($this->publicId,$this->systemId);
		$chunkDoc->loadHTML($chunk);
		$chunkNode = $this->domDoc->importNode($chunkDoc->documentElement);
		if(!$parentNode->appendChild($chunkNode)) {
			throw new Exception('Appending of chunk failed.');
		}
	}
}
?>
Menu::__construct() [class Menu extends DOMDocument]{menu items read from XML file}
<?php
function __construct($file)	{
	parent::__construct();
	if(!parent::load($file)){
		trigger_error('Failed loading menu.',E_USER_WARNING);
		exit();
	}
	$this->filePath = $file;
	$this->validateOnParse = true;
	$this->preserveWhitespace = false;	
	$this->formatOutput = true;
}
?>
Menu::appendMenu()
<?php
public function appendMenu($name,$selectedItem,$imgLife=86400,$subdir=0)//img life default 24h.
{
	global $backbone;
	if(!$subdir) $subdir = $name;
	if($selectedItem == 'firstChild') {
		$parentNode = parent::getElementsByTagName($name)->Item(0);
		foreach ($parentNode->childNodes as $node) {
			if($node->nodeType == XML_ELEMENT_NODE) {
				$selectedItem = $node->nodeName;
				break;
			}
		}
	}
	$items = self::getItems($name);
	$unselectedItems = array_diff($items,array($selectedItem));

	if($name !== 'mainmenu') {	
$parentNode = $backbone->getElementById('subnavcontainer');
echo 'parentnode: '.$parentNode->getAttribute('id');
				$sc = $backbone->appendElement($parentNode,'div',array('id'=>'selectedcontainer'));
				$usc = $backbone->appendElement($parentNode,'div',array('id'=>'unselectedcontainer'));
				foreach($items as $item) {
					if($item == $selectedItem) {
						$layer = $backbone->appendElement($sc,'div',array('id'=>$item));
						$backbone->appendElement($layer,'img',
							array('src'=>'img.php?f=menu/'.$subdir.'/'.$item.'.gif','alt'=>''));
						$backbone->appendElement($layer,'div',array('id'=>$item.'line','class'=>'subnavselectedline'));
					}
						else {
						$layer = $backbone->appendElement($usc,'div',array('id'=>$item,'class'=>'subnav'));
						$backbone->appendElement($layer,'img',
							array('src'=>'img.php?f=menu/'.$subdir.'/'.$item.'.gif','alt'=>''));
						$backbone->appendElement($layer,'div',array('id'=>$item.'line','class'=>'subnavline'));
					}
				}
	}
		else {
		$parentNode = $backbone->getElementById('menucontainer');
		foreach($items as $item) {
			if($item == $selectedItem) {
				$src = 'img.php?f=menu/'.$subdir.'/'.$item.'selected.gif';
			}
				else {
				$src = 'img.php?f=menu/'.$subdir.'/'.$item.'.gif';	
			}
			$backbone->appendElement($parentNode,'img',array('alt'=>'','src'=>$src));
		}
	}
}
?>
DomXml::appendElement() [global backbone->appendElement()]
<?php
public function appendElement($parentElement,$tag,$attrArr=array(),$text=false){
	if(!is_object($parentElement)){
		throw new Exception('parentElement not an Object.');
	}
	if($text) {
		$element = $this->domDoc->createElement($tag,$text);
	}
		else {
		$element = $this->domDoc->createElement($tag);
	}
	if(!$parentElement->appendChild($element)){
		throw new Exception('Appending of element failed.');
	}
	if(!empty($attrArr) && count($attrArr)) {
		foreach($attrArr as $attrName=>$attrValue){
			$element->setAttribute($attrName,$attrValue);
		}
	}
	return $element;
}
?>


#4 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 23 August 2006 - 08:26 AM

From the manual:

DOMDocument->createElement()

.....

Return Values

Returns a new instance of class DOMElement or FALSE if an error occured.


Right now it returns no node nor false.. (but NULL)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users