The main problem is that cloneNode only makes a shallow copy; in fact you can still use the node $dead after it is unbound and it still contains all the child info. Here is my rewritten program:
#!/usr/bin/perl -w use strict; use XML::LibXML; my $parser = XML::LibXML->new(); my $doc = $parser->parse_string(join "", <DATA>); # copy the node, then delete it my $from_path = q{/category/subcategory/product[product_id = "800.55.0 +1"]}; my @from_nodes = $doc->findnodes( $from_path); my $dead = $from_nodes[0]; # Assume there can be only one $dead->unbindNode; # paste the node to a new location my $to_path = q{/category/subcategory[@name = "Grand Arabica"]}; my @to_nodes = $doc->findnodes( $to_path); my $paste = $to_nodes[0]; # Assume there can be only one my $lastnode = $paste->lastChild(); $paste->insertAfter( $dead, $lastnode); print $doc->toString([0]);
-Mark
In reply to Re: XML::LibXML cut and paste node
by kvale
in thread XML::LibXML cut and paste node
by Maxim
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |