Jump to content

Dom trouble


448191

Recommended Posts

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

[code]<?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;
}
?>[/code]

Now it does:
[quote]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 [b]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 [/b]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[/quote]

This leads me to this code (in Menu::appendMenu()):
[code]<?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'));
}
}
}
?>[/code]

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

[size=13pt][b]Does ANYONE have ANY idea why DomXml::appendElement() returns NULL instead of the element appended???[/b][/size]
Link to comment
Share on other sites

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...
Link to comment
Share on other sites

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

[b]index2.php[/b]
[code]<?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');
?>[/code]
[b]DomXml::__construct() [/b][Backbone::Page::DomXhtml::DomXml->__construct()]
[code]<?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;
}
?>[/code]
[b]DomXml::appendChunk() [/b][Backbone::Page::DomXhtml::DomXml->appendChunk()]
[code]<?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.');
}
}
}
?>[/code]
[b]Menu::__construct()[/b] [class Menu extends DOMDocument][i]{menu items read from XML file}[/i]
[code]<?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;
}
?>[/code]
[b]Menu::appendMenu()[/b]
[code]<?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));
}
}
}
?>[/code]
[b]DomXml::appendElement() [/b][global backbone->appendElement()]
[code]<?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;
}
?>[/code]
Link to comment
Share on other sites

From the [url=http://nl3.php.net/manual/en/function.dom-domdocument-createelement.php]manual[/url]:
[quote]DOMDocument->createElement()

.....

Return Values

Returns a new instance of class DOMElement or FALSE if an error occured. [/quote]

Right now it returns no node nor false.. (but NULL)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.