
XML::Simple::Tree - Tree object extension for XML::Simple data structures

## script 1
## create XML::Simple::Tree object and do a preorder traversal
## create XML::Simple::Tree object from an xml document ($xml_file)
my $xml_obj = XML::Simple::Tree->new(file => 'directory.xml',
node_key => 'dir',
target_key => 'name');
## sub set_do_node() method takes subroutine reference to be executed at current node
$xml_obj->set_do_node(
sub {
my $self = $xml_obj;
my $cnode = $self->get_cnode();
my $level = $self->get_level();
my $padding = '* ' x ($level + 1);
print "$padding$cnode->{name}[0]\n";
}
);
## sub set_do_leaf() method takes subroutine reference to be executed at leaf node
$xml_obj->set_do_leaf(
sub {
my $self = $xml_obj;
print "\n";
}
);
## Tree pre order traversal method that executes do_node() at each node and do_leaf() at each leaf
$xml_obj->traverse();
## script 2
## find a node and retrieve a parameter.
my $xml_obj =
XML::Simple::Tree->new(
file => $xml_file,
node_key => 'directory',
target_key => 'name'
);
my $want_node = $xmlObj->find_node($target_directory);
my $mtime = $want_node->{mtime}[0];
## script 3
## find a node and cut (remove) it from tree.
my $cut_name = 'bin';
my $mainXml =
XML::Simple::Tree->new( file => $xml_file,
node_key => 'directory',
target_key => 'name');
$mainXml->cut_node($cut_name);
## script 4
## take XML::Simple::Tree object and paste it into a target node of another
## convert it back to xml
my $target_dir = 'xxx';
my $cut_tree =
XML::Simple::Tree->new(file => $cut_xml_file,
node_key => 'directory',
target_key => 'name');
$config_tree->paste_node($target_dir, $cut_tree->get_cnode()->{directory}[0]);
## convert to xml
my $xml = $config_tree->toXML();
## Additional examples can be found in the included tests.

This module extends XML::Simple by taking the data structure returned by XML::Simple::XMLin($xml_file, forcearray => 1) and putting it in a class complete with tree manipulation and traversal methods. Important to know is that XMLin is called with the option ForceArray => 1. This option forces nested elements to be represented as arrays even when there is only one.

required parameters
finds and returns node where $name matches 'target_node'
returns list of child node[s] of current node. Current node is defined in paramater 'cnode'
returns list of sibling nodes relative to current node ('cnode'). List includes current node. Once again current node is stored in 'cnode'. Simply iterates through children of 'pnode', parent node of cnode.!
finds and splices away specified node
finds node specified by $name and swaps adjacent node (up -> -1, down -> +1) useful for reordering children
finds copies( dclone() ) and returns wanted node
finds destination node and pastes (push()) paste_node in place
pre-order traversal which walks tree executing do_node() and do_leaf where appropriate
post-order traversal which walks tree executing do_node() and do_leaf where appropriate
sets subroutine reference executed at each node
sets subroutine reference executed at each leaf
returns true if current node has no children, and false otherwise.
Returns XML representation of object instance tree. By default it returns XML for the root node (rnode). The default behavior can be overridden by supplying another node reference as an argument. This method is implemented as a wrapper method for XML::Simple::XMLout(), so any options for that subroutine should carry over. See XML::Simple documentation for more information.

sets parent node field
sets wanted node field
sets current node field
sets current level field
sets position of node in pnode
gets current node field
gets target_key field
gets node_key field
gets parent node field
gets root node field
returns level field
returns node position in pnode
returns xml filename. Field 'file' is set only if passed to new as new(file => $xmlfile)
returns xml in string form. Field 'string' is set only if passed to new as new(string => $xmlstr)

Aaron Dancygier, <aaron@dancygier.com>

Copyright 2005 by Aaron Dancygier
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.