in reply to Swapping XML Elements
use strict; use warnings; use XML::Twig; my $infile = "in-test.xml"; my $outfile = "out-test.xml"; open(OUT, ">$outfile") or die "can\'t open output file $outfile:$!"; open(IN, "$infile") or die "can\'t open input file $infile:$!"; my $t = XML::Twig->new ( twig_roots => {'figure' => \&doSwap}, twig_print_outside_roots => \*OUT, ); $t->parse (\*IN); close OUT or die "Could not close $outfile"; close IN or die "Could not close $infile"; sub doSwap { my ($t, $figure) = @_; # required argumen +ts for twig handlers my @title = $figure->cut_children ('title'); # extract (the onl +y) <title> node from the <figure> node $title[0]->paste (last_child => $figure); # make it the last + child of that <figure> node $figure->print(\*OUT); # print the rearra +nged <figure> node }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Swapping XML Elements
by peace (Novice) on Dec 05, 2005 at 15:50 UTC |