Generate Custom XML with Perl

Written by James McDonald

December 28, 2011

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

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

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 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"),

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


Submit a Comment

Your email address will not be published.

You May Also Like…


Zebra Printer Language The above website has a page where you can enter ZPL and it...