SimpleXMLIterator

php.internals

Mathieu Bruneau

22 years ago
Hi all, I have been playing with the SPL lately and today I ended up playing with the SimpleXMLIterator. The documentation on that class if fairly poor and I'm in fact requesting some information about it. I managed to create a SimpleXMLIterator passing it basic xml in the constructor but wasn't able to get anything from the getChildren() method and hasChildren() return false. The var_dump of the iterator show me the expected thing tough. Nor does the current() or key() return anything. I'm using php5.0.1 Example: <?php $xml = "<test><test1>Here we are</test1><test2>There</test2></test>"; $it = new SimpleXMLIterator($xml); var_dump($it); while($it->hasChildren()) { echo "While\n"; $child = $it->getChildren(); } echo "Begin\n"; echo "Key : ".$it->key()."\n"; echo "Current :".$it->current()."\n"; echo "End\n"; ?> And here's what is outputted object(SimpleXMLIterator)#1 (2) { ["test1"]=> string(11) "Here we are" ["test2"]=> string(5) "There" } Begin Key : Current : End I tried with some other combination of possible xml but nothing worked so far. Any things I'm doing wrong ?
-- Math aka ROunofF ====== argontechnologies.ca

Adam Maccabee Trachtenberg

22 years ago
On Sat, 28 Aug 2004, Mathieu Bruneau wrote:
> I have been playing with the SPL lately and today I ended up playing > with the SimpleXMLIterator. >
[snip]
> Example: > <?php > > $xml = "<test><test1>Here we are</test1><test2>There</test2></test>"; > > $it = new SimpleXMLIterator($xml);
foreach ($it as $k => $v) print "$k: $v\n"; test1: Here we are test2: There -adam
-- adam@trachtenberg.com author of o'reilly's "upgrading to php 5" and "php cookbook" avoid the holiday rush, buy your copies today!

Marcus Börger

22 years ago
Hello Mathieu, Sunday, August 29, 2004, 4:18:25 AM, you wrote:
> Hi all,
> I have been playing with the SPL lately and today I ended up playing > with the SimpleXMLIterator.
> The documentation on that class if fairly poor and I'm in fact > requesting some information about it. I managed to create a > SimpleXMLIterator passing it basic xml in the constructor but wasn't > able to get anything from the getChildren() method and hasChildren() > return false. The var_dump of the iterator show me the expected thing tough.
> Nor does the current() or key() return anything. I'm using php5.0.1
> Example: > <?php
> $xml = "<test><test1>Here we are</test1><test2>There</test2></test>";
> $it = new SimpleXMLIterator($xml);
> var_dump($it);
> while($it->hasChildren()) > { > echo "While\n"; > $child = $it->getChildren(); > }
Try working with while($it->valid().... Besides that SimpleXMLIterator implements the RecursiveIterator interface. What it does is iterating over one element and if teh element has sub elements then hasChildren() returns true and getchildren() returns the iterator for that sub element. To see what's going on have a look at ext/spl/tests/sxe_00*.phpt. regards marcus

Mathieu Bruneau

22 years ago
Marcus Boerger wrote:
> Hello Mathieu, > > Sunday, August 29, 2004, 4:18:25 AM, you wrote: > > >>Hi all, > > >> I have been playing with the SPL lately and today I ended up playing >>with the SimpleXMLIterator. > > >> The documentation on that class if fairly poor and I'm in fact >>requesting some information about it. I managed to create a >>SimpleXMLIterator passing it basic xml in the constructor but wasn't >>able to get anything from the getChildren() method and hasChildren() >>return false. The var_dump of the iterator show me the expected thing
tough.
> > >>Nor does the current() or key() return anything. I'm using php5.0.1 > > > >>Example: >><?php > > >>$xml = "<test><test1>Here we are</test1><test2>There</test2></test>"; > > >>$it = new SimpleXMLIterator($xml); > > >>var_dump($it); > > >>while($it->hasChildren()) >>{ >> echo "While\n"; >> $child = $it->getChildren(); >>} > > > Try working with while($it->valid().... > Besides that SimpleXMLIterator implements the RecursiveIterator
interface.
> What it does is iterating over one element and if teh element has sub > elements then hasChildren() returns true and getchildren() returns the > iterator for that sub element. To see what's going on have a look at > ext/spl/tests/sxe_00*.phpt. > > regards > marcus > >
I already tried that but I figured out what I was doing wrong... You need to rewind the iterator before using it... I don't know why but I was assuming that it was ready for a loop! The other solution foreach is working as the foreach does a rewind() on it at first.
-- Math aka ROunofF ====== argontechnologies.ca