Re: DOMDocument->getElementById() isn't working

From: Date: Sun, 06 Jan 2008 16:37:23 +0000
Subject: Re: DOMDocument->getElementById() isn't working
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-266826@lists.php.net to get a copy of this message
Christoph Boget wrote: > Getting the elements by tag name, while iterating through the list I see > that one of the nodes has an id of 'custom'. However, when I try to get the > element directly using getElementById(), it doesn't return the node > properly. Am I doing something wrong? A common problem. See here: http://wiki.flux-cms.org/display/BLOG/GetElementById+Pitfalls Probably easiest to use XPath. Otherwise you have to slightly modify your HTML... e.g. replace your getElementById with: $xp = new DomXPath($oDOMDocument); $oCustomNode = $xp->query("//*[@id = 'custom']")->item(0); > Also, as an aside, one thing that I found odd is that count( $aLayoutNodes ) > shows as 1 even though more are found. Huh? That's because $aLayoutNodes is not an array. It is a DOMNodeList object. That means it implements the Iterator interface which means it works with foreach, but not with e.g. count(). Use $aLayoutNodes->length to see how many nodes there are in the list (i.e. use that as a direct replacement for count($aLayoutNodes)) (also if the "a" prefix is meant to identify the var as an array I'd probably change it to the "o" prefix you seem to use for objects...) Col

« previous php.general (#266826) next »