Thanks. You guys are terrific! I wasn't keen on using regular expressions,
so the Twigs module was right on target.
Here's the code I ended up using to fix up my 3MB XML document.
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
}
Peace,
--Jack
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.