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

I'm trying to produce a simple XML document from the CGI parameters submitted in a form. This is basically what I'm doing:

my $xs = new XML::Simple(); my $elements = $cgi->Vars(); my $xml = $xs->XMLout( $elements, rootname => 'source' );

So $elements is a reference to a hash where the key is the CGI parameter and the value is the value associated with that parameter. The CGI parameters I don't want in the XML are prefixed with "-" which according to the XML::Simple docs should omit them from the XML.

The problem is the contents of $xml look like this: <source></source> ie no elements get written into the output. Can anyone offer some advice as to what I have missed?

Thanks.

Replies are listed 'Best First'.
Re: XML from CGI parameters
by BrowserUk (Patriarch) on Jul 23, 2002 at 07:02 UTC

    I think the problem is that the hashref returned from CGI::Vars() is a tied hashref, which XMLout doesn't seem to like. Making a copy of it as shown below seems to 'avoid' the problem.

    #! perl -w use strict; use CGI; use XML::Simple; my $cgi= new CGI; my $xs = new XML::Simple(); my $elements = $cgi->Vars(); my %copy = %$elements; my $xml = $xs->XMLout(\%copy, rootname => 'source'); print $xml; __DATA__ #output C:\test>184342 "test=1&fred=bill&something=somethingelse" '<source fred="bill" something="somethingelse" test="1" />

    Update: Removed redundant use of Data::Dumper.

      It wouldn't be Perl if you couldn't do without intermediate variables: my $xml = $xs->XMLout({%{$cgi->Vars()}}, rootname => 'source'); ____________
      Makeshifts last the longest.
Re: XML from CGI parameters
by PodMaster (Abbot) on Jul 23, 2002 at 07:03 UTC
    consider the following code
    #!/usr/bin/perl -w use strict; use warnings; use CGI; use Data::Dumper; use XML::Simple; my $cgi = new CGI( { a => 1, b=> 2, source => 3 } ); my $elements = $cgi->Vars(); DOIT(); $elements = { 'source' => { 'a' => '1', 'b' => '2', } }; DOIT(); $elements = { 'butterscotch' => { 'a' => '1', 'b' => '2', }, 'content' => "Look at me Maa, I'm eating buttterscotch", }; DOIT(); sub DOIT { my $xs = new XML::Simple(); my $xml = $xs->XMLout( $elements, rootname => 'source' ); print Dumper $elements; print $xml; print '='x60,"\n"; } __END__ $VAR1 = { 'a' => '1', 'b' => '2', 'source' => '3' }; <source></source> ============================================================ $VAR1 = { 'source' => { 'a' => '1', 'b' => '2' } }; <source> <source a="1" b="2" /> </source> ============================================================ $VAR1 = { 'butterscotch' => { 'a' => '1', 'b' => '2' }, 'content' => 'Look at me Maa, I\'m eating buttterscotch' }; <source>Look at me Maa, I'm eating buttterscotch<butterscotch a="1" b= +"2" /> </source> ============================================================
    Now please go and read the XML::Simple page a little more, and experiment (cause I can't provide better explanation, sorry).

    Your best bet is to invoke XMLout like

    my $xml = $xs->XMLout( { content => 'I am content', thecgi => $elements, PARAM => [ $cgi->param('PARAM') ], }, rootname => 'source', );
    update: Following BrowserUks example above, just forget about the intermediate hash, and invoke Vars like
    my %elements = $cgi->Vars();
    So you don't have any problems.

    ____________________________________________________
    ** The Third rule of perl club is a statement of fact: pod is sexy.