|
Posted by "Jared Williams" on 05/28/05 20:32
> -----Original Message-----
> From: Victor Spång Arthursson [mailto:victor@tosti.dk]
> Sent: 27 May 2005 17:25
> To: php-general@lists.php.net
> Subject: [PHP] DOM: browse childnodes but not recursively
>
> Ciao!
>
> I really hope someone can help me on this, since I have been
> putting in to much time in it now, and I have to show off
> some results ;)
>
> The problem is that I can't browse nodelists in only one
> dimension, that is, whitout getting the sub-nodes of the nodes.
>
> My XML reads:
>
> <element id="5">
> <element id="51">Still got the blues</element>
> <element id="52">Gary Moore</element>
> <element id="53">
> <element id="9">
> <element id="531">Maggie May</element>
> <element id="532">Rod Stewart</element>
> <element id="533">UK</element>
> <element id="534">Pickwick</element>
> <element id="535">8.50</element>
> <element id="536">1990</element>
> </element>
> </element>
> <element id="54">Virgin records</element>
> <element id="55">10.20</element>
> <element id="56">1990</element>
> </element>
>
> I get this as a DOMNodeList in the variable $elements. I will
> write some examples, to describe my problem.
>
> echo $elements->length;
> // outputs 1
>
> var_dump($elements);
> // outputs object(DOMNodeList)#5 (0) { }
>
> var_dump($elements->item(0)); // contents of element with id="5"
> // outputs object(DOMElement)#4 (0) { }
>
> Here I come to the problem. What I want is to get a list of
> the 6 elements inside element id="5", but not with child-childs.
>
> echo $elements->item(0)->childNodes->length;
> // Outputs 13!!!!!
childNodes contains the textnodes too, in this case the whitespace between each of <element>.
> So, to put it in short words; how do I do to browse the
> content of the element id="5" withuot doing it recursively? I
> want to receive a list when I call childNodes (or equivalent)
> that gives me the elements with id 51-56, and a length of 6.
>
for($child = $elements->item(0)->firstChild; $child; $child = $child->nextSibling)
if ($child->nodeType == XML_ELEMENT_NODE)
echo $child->getAttribute('id'), "\n";
Jared
Navigation:
[Reply to this message]
|