Page 1 of 1
DOMElement
Posted: Tue Jan 20, 2009 5:23 am
by mintedjo
Hello. I just posted this in the wrong forum but discovered i could delete it so i reposted in the right bit

Here is my problem.
I get a DOMElement
Code: Select all
$ref_cell = $new_row->childNodes->item($j);
I access the properties of said element
Computer says...
Trying to get property of non-object...
I try
Computer says...
Object of class DOMElement could not be converted to string...
Can anybody explain this?
Any insight at all would be helpful - I'm about to rip my face off and put my the keyboard thru the monitor...
EDIT: And also if you know anything about the php DOM is there a way to get all the attributes and corresponding values of a DOMElement. Thx
Re: DOMElement
Posted: Tue Jan 20, 2009 9:55 am
by Burrito
try a print_r() or var_dump() on your object to see what it shows you.
Re: DOMElement
Posted: Tue Jan 20, 2009 10:07 am
by mintedjo
var dumps fail on DOMElements.
I've included a demonstration below.
The variable used is the same one that isn't working in my OP. Bear in mind that I can print the DOMElement and its contents using
Code: Select all
$ref_cell->ownerDocument->saveXML($ref_cell);
Now here is a list of failed attempts at var dumps.
Code: Select all
//var_dump($ref_cell);
object(DOMElement)#19 (0) {}
//print_r($ref_cell);
DOMElement Object()
//serialize($ref_cell)
O:10:"DOMElement":0:{}
//json_encode($ref_cell);
{}
Any more ideas?
Re: DOMElement
Posted: Wed Jan 21, 2009 3:50 am
by mintedjo
Sorry for the bump but....
If anybody has any ideas on either of the things I mentioned above pleeeeese reply - its kindof important

Re: DOMElement
Posted: Wed Jan 21, 2009 7:45 am
by mintedjo
I'm getting closer...
I managed to get some of the stuff working now by hacking at it with a big rusty spoon.
However... occasionally doing this
returns null and I can't figure out why. The element is nested in another element - I can see that it has the correct structure by printing the whole document using saveXML().
I've decided to post a function this time because maybe i'm doing something utterly retarded somewhere. The lines which cause problems are colored.
Code: Select all
/* Changes the tagname of a DOMElement node - incomplete */
function changeTagName(&$dom_element, $new_tag_name){
/* Create a new element with the required tag name */
$replacement = $dom_element->ownerDocument->createElement($new_tag_name);
/* Copy all the sub elements */
$children = $dom_element->childNodes;
for($i = 0; $i < $children->length; $i++){
$replacement->appendChild($children->item($i));
}
[color=#FF00FF]$dom_element->parentNode->replaceChild($replacement, $dom_element);[/color]// ParentNode is null
//I also tried doing the following line when parentNode was null (I assumed that would mean it was the root element)
[color=#FF0000]$dom_element->ownerDocument->replaceChild($replacement, $dom_element);[/color]
$dom_element->ownerDocument->saveXML(); // This line prints the whole document how I expect it to look
}
The error i get for the red line is this one: which (apparently) means that the old element is not a (immediate?) child of the node which calls the replace function.
computer wrote:Uncaught exception 'DOMException' with message 'Not Found Error'
Any ideas?

I don't see how both parentNode can be null AND not be an immediate child of the document - ie. a root element.
Re: DOMElement
Posted: Wed Jan 21, 2009 8:44 am
by mintedjo
Nevermind... I found it on another website after some googling.
The node i was working on was a copy of a node which was already in the document.
I used cloneNode to make an identical copy to work on.
I was forgetting to insert the element into the document so although the object is valid, is a member of the DOMDocument; it didn't have a position in the document. Hence no parentNode.