in reply to XML:Twig -- changing in-mem process to stream

Wahouh! You sure do things the hard way!

Below is a version that loads only one b at a time.

To send the XML to a file just pass a filehandle ref to flush or print: $t->flush( \*FILE).

#!/usr/bin/perl -w use strict; use XML::Twig; my $t = XML::Twig->new( twig_handlers => { b => \&b, }, pretty_print => 'indented', ); $t->parse( \*DATA); $t->flush; sub b { my ( $t, $b ) = @_; foreach my $c ($b->children('c')) { # yep, that does it: wrap a b element around the c $c->wrap_in( b => { name => $c->att( 'name') } ) } $b->erase; # remove the original b $t->flush; # you need to flush here if you want to free the memory } __DATA__ <a> <b name="funny words"> <c name="foo"/> <c name="baz"/> </b> <b name="foods"> <c name="apple"/> <c name="pear"/> <c name="cheese"/> </b> </a>