AzaBat has asked for the wisdom of the Perl Monks concerning the following question:

I can't figure out how to use XML::Generator's xmldecl method. to generate the following tag:

<?xml version='1.0' encoding='utf-8'?>

The doc's just say "This can be used to specify the version, encoding, and other XML-related declarations (i.e., anything inside the <?xml?> tag)". Nothing else I've found has helped with this method.

I've tried messing around with conformance and escape but nothing I seems to work.

Example:

#!/usr/bin/perl -w use strict; use XML::Generator; my $gen = XML::Generator->new( conformance => 'strict', escape => 'always', pretty => 2 ); print $gen->xmldecl(['UTF-8', 'no']), $gen->request( $gen->name('value') );

Yealds:

<?xml version="1.0" standalone="yes"?> <request> <name>value</name> </request>

Thanks in advance for any help, AzaBat

Replies are listed 'Best First'.
Re: XML::Generator's xmldecl
by chromatic (Archbishop) on Jul 29, 2003 at 02:43 UTC

    Looking at the source, you need to specify encoding and version in the constructor, not in the method call:

    my $gen = XML::Generator->new( conformance => 'strict', escape => 'always', pretty => 2, encoding => 'UTF-8', version => '1.0', ); print $gen->xmldecl(), $gen->request( $gen->name('value') );

      Thanks chromatic! Look at the source??? I don't know why I didn't think of that. It's even documented there.

      AzaBat