in reply to XML::Twig outputting root element start tag twice
First, you're missing the end-flush. Before you're really "DONE", you need to add $p->flush(). That gets the extra </foo> tag you're missing in your output. Not that you want it, but once you fix the other problem, you'll want it back.
Second, it's the twig_print_outside_roots flag that's doing it. Remove that. Instead, change your flush calls (including the new one) to have the param "$outfh". Now you'll flush to that file.
That leaves me with:
As to why, ... I'm not sure.#!/usr/bin/perl use strict; use warnings; use XML::Twig; open my $outfh, '>', "out.xml" or die ">out.xml:$!"; my $p = XML::Twig->new( #twig_print_outside_roots => $outfh, twig_roots => { record => sub { $_->set_text("altered ".$_->text); shift->flush($outfh), } }, empty_tags => 'html', keep_encoding => 1, keep_spaces => 1, ); $p->parsefile("in.xml"); $p->flush($outfh); print "DONE\n";
Hope that helps,
Update: Ok, I see you really want the twig_print_outside_roots feature. It doesn't seem to do what you want it to, though. I am curious, though, as to why the formatting matters - this is XML, after all...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML::Twig outputting root element start tag twice
by benizi (Hermit) on Apr 18, 2006 at 19:06 UTC | |
by mirod (Canon) on Apr 19, 2006 at 06:06 UTC | |
by benizi (Hermit) on Apr 25, 2006 at 15:20 UTC |