Generate Custom XML with Perl CGI.pm

Written by James McDonald

December 28, 2011

I want to output custom XML without any HTML or DTD tags using CGI.pm

Here is my hack to get what I’m after:

#!/usr/bin/perl
use strict;

use CGI qw(:standard -any); 
# use the -any option to turn on custom tags
# e.g. I can use $r->item("Item Content") to create
# Item Content
# or start_item, end_item syntax

my $r =  CGI->new;

# returns an xml document
print $r->header(-type=> 'text/xml'),
        '< ?xml version="1.0" encoding="UTF-8"?>',
        # I couldn't get CGI.pm to output the normal < ?xml> block
        # using -declare_xml so just insert it as above into the print
        # statement
        $r->start_custom ( { key1=>'value1', key2=>'value2', key3=>'value3' }  ),
        "This is a custom tag" ,
        $r->product({ speed=>'slow', start=>'1', end=>'10'}, "product tags"),
        $r->end_custom;     

Note: For some reason a space is inserted by WordPress or wp-syntax in the <?xml ?> tag above and below.

Output here:

< ?xml version="1.0" encoding="UTF-8"?>

	This is a custom tag
	product tags

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

You May Also Like…

How to Research a CPU Upgrade

How to Research a CPU Upgrade

Upgrade Time! Doing a lot of VMWare Workstation virtualization to create labs for self-study and training. Finding...