Re: [PHP] DOM: browse childnodes but not recursively

2005-05-30 Thread Victor Spång Arthursson


28 maj 2005 kl. 19.32 skrev Jared Williams:

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;


Thanks for the answer!

It doesn't really work in my case, since I'm trying to create a  
recursive function according the following:


1. I perform a search using xPath and get a search result as a  
DOMNodeList.


2. This list I send to a function that takes it as input, and whats  
function is to create a multidimensional array.


3. Foreach item in the list I check if it has any children.

4. If so, I do the recursive call, with the list of children as input.

So far so good, but when I come to the second loop, when the input is  
the list from the childNodes, everything stops working...


Sincerely

Victor


RE: [PHP] DOM: browse childnodes but not recursively

2005-05-28 Thread Jared Williams
 

 -Original Message-
 From: Victor Spång Arthursson [mailto:[EMAIL PROTECTED] 
 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=51Still got the blues/element
  element id=52Gary Moore/element
  element id=53
  element id=9
  element id=531Maggie May/element
  element id=532Rod Stewart/element
  element id=533UK/element
  element id=534Pickwick/element
  element id=5358.50/element
  element id=5361990/element
  /element
 /element
  element id=54Virgin records/element
  element id=5510.20/element
  element id=561990/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 

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php