Just wrap the code to process a node in a function and call it recursively. Example with some additional comments:
sub process_node {
my $node = shift;
print $node->nodePath, "\n";
# No need to check hasChildNodes. If there aren't any
# children, childNodes will return an empty array.
for my $child ($node->childNodes) {
# Call process_node recursively.
process_node($child);
}
}
# documentElement is more straight-forward than findnodes('/').
process_node($root->documentElement);