in reply to replace node in xml

Using XML::LibXML you can do (assuming you've put the XML in test.xml):

#!/usr/bin/env perl use strict; use warnings; use XML::LibXML; my $doc = XML::LibXML->new()->parse_file('test.xml'); my $new_parent = ($doc->findnodes('//module [@name="all"]'))[0]; my @nodes = map {$_->getAttribute('name')} $doc->findnodes('//module [ +@name="all"]/moduleref'); my ($old_node, $new_node); foreach (@nodes) { $old_node = ($doc->findnodes('//moduleref [@name="' . $_ .'"]') )[ +0]; $new_node = ($doc->findnodes('//module [@name="'.$_ .'"]'))[0]; next unless $new_node; $old_node = $new_parent->replaceChild($new_node, $old_node); } print $doc->toString;

Replies are listed 'Best First'.
Re^2: replace node in xml
by joeperl (Acolyte) on Dec 27, 2009 at 08:27 UTC

    hi waldo,
    thanks for your help, but im using XML::DOM, so what ikegami suggested works fine.
    couldnt run your code since LibXML is not installed in office..
    still thanks for your help..