Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?


by Masem (Monsignor)
on Feb 03, 2002 at 17:17 UTC ( #143079=sourcecode: print w/ replies, xml ) Need Help??

Category: XML Utilities
Author/Contact Info Michael K. Neylon (
Description: This module converts CGI objects to an XML structure. While there is CGI::XML out there, the job it does isn't really great in light of newer additions to the XML family. The XML that this produces (see the pod for example), IMO, is more usable for, say, getting the CGI used to generate a page during a XSLT transformation that what CGI::XML offered. C&C is appriciated on this as well as additional functionality that might be wanted.

#!/usr/bin/perl -w
package CGI::ToXML;

# $Id:,v 0.01 2002/02/03 17:11:44 mneylon Exp $
# $Revision: 0.01 $
# $Author: mneylon $
# $Date: 2002/02/03 17:11:44 $
# $Log:,v $
# Revision 0.01  2002/02/03 17:11:44  mneylon
# Initial release to Perlmonks

use strict;
use XML::Simple;

  use Exporter   ();
  $VERSION     = sprintf( "%d.%02d", q( $Revision: 0.01 $ ) =~ /\s(\d+
+)\.(\d+)/ );
  @ISA         = qw(Exporter);
  @EXPORT      = qw();
  @EXPORT_OK   = qw( CGItoXML );
  %EXPORT_TAGS = (  );

sub CGItoXML {
  my ( $cgi, %options ) = @_;
  if ( ! $cgi->isa( "CGI" ) ) {
    warn "CGItoXML: Object isn't a CGI object, cannot convert";
    return undef;

  # Determine which parameters we keep or not
  my @paramlist = $cgi->param;

  # Exclude what we can first...
  if ( exists $options{ exclude } ) {
    my %exclude_hash; $exclude_hash{ $_ }++ foreach @{ $options{ exclu
+de } };
    @paramlist = grep { !exists( $exclude_hash{ $_ } ) } @paramlist;

  # Include what we can...
  if ( exists $options{ include } ) {
    my %include_hash; $include_hash{ $_ }++ foreach @{ $options{ inclu
+de } };
    @paramlist = grep { exists( $include_hash{ $_ } ) } @paramlist;

  # Set up the hashes to be used for conversion

  my @params;
  foreach my $param ( @paramlist ) {
    my @values;
    # Ensure we get values as an array
    foreach my $value ( $cgi->param( $param ) ) {
      push @values, $value;
    push @params, { parameter => { name => $param,
                   value => \@values } };

  my %cgi_hash = ( cgi => {
               generator => "CGI::toXML",
               version => $VERSION,
               parameter => \@params } );

  return XMLout( \%cgi_hash, rootname => undef ) ;

# Below is stub documentation for your module. You better edit it!

=head1 NAME

CGI::ToXML - Converts CGI to an XML structure


  use CGI::ToXML qw( CGItoXML );
  use CGI;

  my $q = new CGI;
  my $xml = CGItoXML( $q );
  my $xml2 = CGItoXML( $q, exclude => [ password username sessionid ] 


Converts a CGI variable (from to an XML data structure.  While
there is currently a similar module, CGI::XML, by Jonathan Eisenzopf,
the outputted XML is not in a very usable format, given the newer adva
in XML (such as XSLT, XPath, etc).  CGI::ToXML aims to correct this by
providing a cleaner XML structure that will be more useful.  The XML i
generated from XML::Simple, keeping this a 'lightweight' function.

The module consists of a single function:

  $xml = CGItoXML( $cgi, %options )

$cgi must be a valid object, and if not, a warning will be issu
and the function will return undef.  Otherwise, the function will retu
the XML as a string.  The XML structure will be similar to the followi

  <cgi version="0.01" generator="CGI::toXML">
    <parameter name="dinosaur">
    <parameter name="color">

as generated from the query string:


The order of parameters and multivalued entries, as returned by CGI, i
maintained in the XML.

The options hash can be used to customize the behavior a bit more:

=over 4

=item include => [ list ]

Only include the parameters and their values specified in the given li
all other parameters are not included.  Note that this does not affect
+ the
CGI object storage.

=item exclude => [ list ]

Do not include the parameters and their values specified in the given 
all other parameters are included.  Note that this does not affect the
CGI object storage.


=head1 EXPORT

No functions are exported by default, but CGItoXML can be exported by
the user.

=head1 AUTHOR

Michael K. Neylon, E<lt>mneylon-pm@masemware.comE<gt>

=head1 SEE ALSO

L<perl>, L<CGI::XML>, L<XML::Simple>.


Comment on CGI::ToXML
Download Code

Back to Code Catacombs

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://143079]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2015-11-27 18:33 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (731 votes), past polls