Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Cheap XML Append In Perl

DZone's Guide to

Cheap XML Append In Perl

·
Free Resource
Here is a really cheap way in Perl to append an XML fragment to an existing XML doc. It appends it just before the last closing tag. Note that there is no validation or well-formedness checking. Use at your own risk. That said, it can be handy if you know that your XML is going to be OK.

#!/usr/bin/perl

sub xappend(\$$) { ${$_[0]} =~ s/(<\/[^>]+>\s*)$/$_[1]$1/s }

$a = "
  
   
    foo
   
  ";
xappend($a,"
  
   bar
  ");

print $a; # prints 
  
   
    foo
   
   
    bar
   
  
Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}