Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Cheap XML append in Perl (See related posts)

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 = "<doc><item>foo</item></doc>";
xappend($a,"<item>bar</item>");

print $a; # prints <doc><item>foo</item><item>bar</item></doc>

You need to create an account or log in to post comments to this site.


Click here to browse all 5147 code snippets

Related Posts