  | |  | XML Iterator GOF style | XML Iterator GOF style
2004-02-11 - By Olaf Schmidtmann
Back Hi coders,
you may remember bokels XML iterator which was quite a popular afaik. I
converted it to AS2 and changed it to work with XMLNodes instead of XML.
The class is used like this:
iter = new XMLIteratorGOF(myXML);
result_array = new Array();
for( xmlIter.first(); xmlIter.hasNext(); xmlIter.next()){
var currentNode = xmlIter.current();
result_array.push(currentNode.nodeName);
}
delete iter;
The problem is the hasNext() method which does not allow to retrieve the
last node in xml structure because it returns false when reaching the second
last node.
public function hasNext():Boolean
{
return node.hasChildNodes() || node.nextSibling != null ||
stack.length >0;
}
If you use an XML this misbehavior does not occur because (I guess) there is
something left at the last position. I did not investigate this in detail.
A quick work-around would be to introduce a variable that is set to 0 in the
first() method. The hasNext() method would look like this and - to be exact
- would not deserve its name any more. Anyway, my approach is to get this
iterator to good use.
public function hasNext():Boolean
{
/// KLUDGE: This is too bad! But without the stopper the last
element in a for loop will be left out.
if(!(node.hasChildNodes() || node.nextSibling != null ||
stack.length >0)) stopper++;
return stopper < 2;
}
I do not like this from the looks besides the method name which I want to
keep because the iterators should all be "slaved under " an iterator
interface. Maybe I miss something completely here? Is there a more elegant
solution without changing the whole thing? I 'd welcome any thoughs on this.
I did not post the complete iterator, here because I presume it is common
knowledge but if you like I may very well send it to you for further
investigation.
Cheers
Olaf
--
*** http://www.brainbits.net/ ***
=-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- --
Supported by Fig Leaf Software
=-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- --
Be sure to check the archives and the wiki:
http://chattyfig.figleaf.com/
=-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- --
http://chattyfig.figleaf.com/cgi-bin/ezmlm-cgi?1:mss:103764
=-- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- --
To unsubscribe send a blank e-mail to:
Normal Mode: flashcoders-unsubscribe@(protected)
Digest Mode: flashcoders-digest-unsubscrive@(protected)
|
|
 |