Cheap XML append in Perl
1 #!/usr/bin/perl 2 3 sub xappend(\$$) { ${$_[0]} =~ s/(<\/[^>]+>\s*)$/$_[1]$1/s } 4 5 $a = "<doc><item>foo</item></doc>"; 6 xappend($a,"<item>bar</item>"); 7 8 print $a; # prints <doc><item>foo</item><item>bar</item></doc>
DZone Snippets > beutelevision > xml
13476 users tagging and storing useful source code snippets
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
Thomas Beutel http://www.beutelevision.com
1 #!/usr/bin/perl 2 3 sub xappend(\$$) { ${$_[0]} =~ s/(<\/[^>]+>\s*)$/$_[1]$1/s } 4 5 $a = "<doc><item>foo</item></doc>"; 6 xappend($a,"<item>bar</item>"); 7 8 print $a; # prints <doc><item>foo</item><item>bar</item></doc>
1 2 #!/usr/bin/perl 3 4 use warnings; 5 use strict; 6 use XML::Simple; 7 use DBI; 8 9 my $dbh = DBI->connect('DBI:mysql:MYDATABASE','user','password') 10 or die DBI->errstr; 11 12 # Get an array of hashes 13 my $recs = $dbh->selectall_arrayref('SELECT * FROM contents',{ Columns => {} }); 14 15 # Convert to XML where each hash element becomes an XML element 16 my $xml = XMLout( {record => $recs}, NoAttr => 1 ); 17 18 print $xml; 19 20 $dbh->disconnect;
1 2 <opt> 3 <record> 4 <id>1</id> 5 <entry>1932 - 1939</entry> 6 <modified>2005-04-15 22:24:44</modified> 7 </record> 8 <record> 9 <id>2</id> 10 <entry>Yet another entry</entry> 11 <modified>2005-04-15 22:25:00</modified> 12 </record> 13 <record> 14 <id>3</id> 15 <entry>More stuff here</entry> 16 <modified>2005-04-15 22:25:13</modified> 17 </record> 18 </opt>
1 2 $xml =~ s/\xe2\x80\x99/\'/gs; 3 $xml =~ s/\xe2\x80\x98/\'/gs; 4 $xml =~ s/\xe2\x80\x9c/\"/gs; 5 $xml =~ s/\xe2\x80\x9d/\"/gs;