sub _mergeXML { my $FILE1 = shift; my $FILE2 = shift; my $FILEOUT = shift; my $parser = XML::LibXML->new(); my @wanted; my $genericDom = $parser->load_xml(location => $FILE1); for my $branch_node ($genericDom->findnodes("/arbre/branche")) { $branch_node->setAttribute(type => "conifere"); } push(@wanted, $genericDom->documentElement->findnodes("./*")); my $customDom = $parser->load_xml(location => $FILE2); for my $branch_node ($customDom->findnodes("/arbre/branche")) { $branch_node->setAttribute(type => "resineux"); } push(@wanted, $customDom->documentElement->findnodes("./*")); my $new = XML::LibXML::Document->new( '1.0', 'UTF-8' ); $new->addChild($new->createComment("So, in the end we have a forest...")); my $root = XML::LibXML::Element->new( 'arbre' ); $new->addChild( $root ); $root->addChild( $_ ) for @wanted; my $pp = XML::LibXML::PrettyPrint->new(indent_string => " "); $pp->pretty_print($new); open(FILE,">$FILEOUT") or die $!; print FILE $new->toString;close FILE; }