|
|
XML::LibXML::Iterator - Simple Tree Iteration Class for XML::LibXML
use XML::LibXML; use XML::LibXML::Iterator;
my $doc = XML::LibXML->new->parse_string( $somedata ); my $iter= XML::LibXML::Iterator->new( $doc );
$iter->iterator_function( \&iterate );
# more control on the flow while ( $iter->next ) { # do something }
# operate on the entire tree $iter->iterate( \&operate );
An iterator allows to operate on a document tree as it would be a linear sequence of nodes.
new($first_node)
first()
next()
previous()
last()
current()
index()
XML::LibXML::Iterator knows two types of callback. One is knows as the
iterator function, the other is used by iterate(). The first function
will be called for each call of next()
or previous(). It is used to
find out about the next node recognized by the iterator.
The iterator function has to take two parameters: As the first
parameter it will recieve the iterator object, as second the direction
of the iteration will be passed. The direction is either 1 (for next())
or -1 (for previous()).
The iterators iterate()
function will take a function reference that
takes as well two parameters. The first parameter is again the
iterator object. The second parameter is the node to operate on. The
iterate function can do any operation on the node as
prefered. Appending new nodes or removing the current node will not
confuse the iteration process: The iterator preloads the next object
before calling the iteration function. Thus the Iterator will not find
nodes appended by the iteration function.