home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / ToXML.pm < prev    next >
Encoding:
Perl POD Document  |  2002-06-13  |  4.2 KB  |  159 lines

  1. #!/usr/bin/perl -w
  2. package CGI::ToXML;
  3.  
  4. #=============================================================================
  5. #
  6. # $Id: ToXML.pm,v 0.02 2002/02/05 01:09:18 mneylon Exp $
  7. # $Revision: 0.02 $
  8. # $Author: mneylon $
  9. # $Date: 2002/02/05 01:09:18 $
  10. # $Log: ToXML.pm,v $
  11. # Revision 0.02  2002/02/05 01:09:18  mneylon
  12. # Slight fix in POD docs
  13. #
  14. # Revision 0.01  2002/02/03 17:11:44  mneylon
  15. # Initial release to Perlmonks
  16. #
  17. #
  18. #=============================================================================
  19.  
  20. use strict;
  21. use XML::Simple;
  22.  
  23. BEGIN {
  24.   use Exporter   ();
  25.   use vars       qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  26.   $VERSION     = sprintf( "%d.%02d", q( $Revision: 0.02 $ ) =~ /\s(\d+)\.(\d+)/ );
  27.   @ISA         = qw(Exporter);
  28.   @EXPORT      = qw();
  29.   @EXPORT_OK   = qw( CGItoXML );
  30.   %EXPORT_TAGS = (  );
  31. }
  32.  
  33. sub CGItoXML {
  34.   my ( $cgi, %options ) = @_;
  35.   if ( ! $cgi->isa( "CGI" ) ) {
  36.     warn "CGItoXML: Object isn't a CGI object, cannot convert";
  37.     return undef;
  38.   }
  39.  
  40.   # Determine which parameters we keep or not
  41.   my @paramlist = $cgi->param;
  42.  
  43.   # Exclude what we can first...
  44.   if ( exists $options{ exclude } ) {
  45.     my %exclude_hash; $exclude_hash{ $_ }++ foreach @{ $options{ exclude } };
  46.     @paramlist = grep { !exists( $exclude_hash{ $_ } ) } @paramlist;
  47.   }
  48.  
  49.   # Include what we can...
  50.   if ( exists $options{ include } ) {
  51.     my %include_hash; $include_hash{ $_ }++ foreach @{ $options{ include } };
  52.     @paramlist = grep { exists( $include_hash{ $_ } ) } @paramlist;
  53.   }
  54.  
  55.   # Set up the hashes to be used for conversion
  56.  
  57.   my @params;
  58.   foreach my $param ( @paramlist ) {
  59.     my @values;
  60.     # Ensure we get values as an array
  61.     foreach my $value ( $cgi->param( $param ) ) {
  62.       push @values, $value;
  63.     }
  64.     push @params, { parameter => { name => $param,
  65.                    value => \@values } };
  66.   }
  67.  
  68.   my %cgi_hash = ( cgi => {
  69.                generator => "CGI::toXML",
  70.                version => $VERSION,
  71.                parameter => \@params } );
  72.  
  73.   return XMLout( \%cgi_hash, rootname => undef ) ;
  74. }
  75.  
  76.  
  77. 1;
  78. __END__
  79.  
  80. =head1 NAME
  81.  
  82. CGI::ToXML - Converts CGI to an XML structure
  83.  
  84. =head1 SYNOPSIS
  85.  
  86.   use CGI::ToXML qw( CGItoXML );
  87.   use CGI;
  88.  
  89.   my $q = new CGI;
  90.   my $xml = CGItoXML( $q );
  91.   my $xml2 = CGItoXML( $q, exclude => [ qw( password username sessionid ) ] );
  92.  
  93. =head1 DESCRIPTION
  94.  
  95. Converts a CGI variable (from CGI.pm) to an XML data structure.  While
  96. there is currently a similar module, CGI::XML, by Jonathan Eisenzopf,
  97. the outputted XML is not in a very usable format, given the newer advances
  98. in XML (such as XSLT, XPath, etc).  CGI::ToXML aims to correct this by
  99. providing a cleaner XML structure that will be more useful.  The XML is
  100. generated from XML::Simple, keeping this a 'lightweight' function.
  101.  
  102. The module consists of a single function:
  103.  
  104.   $xml = CGItoXML( $cgi, %options )
  105.  
  106. $cgi must be a valid CGI.pm object, and if not, a warning will be issued
  107. and the function will return undef.  Otherwise, the function will return
  108. the XML as a string.  The XML structure will be similar to the following:
  109.  
  110.   <cgi version="0.01" generator="CGI::toXML">
  111.     <parameter name="dinosaur">
  112.       <value>barney</value>
  113.       <value>godzilla</value>
  114.     </parameter>
  115.     <parameter name="color">
  116.       <value>purple</value>
  117.     </parameter>
  118.   </cgi>
  119.  
  120. as generated from the query string:
  121.  
  122.   "dinosaur=barney&dinosaur=godzilla&color=purple"
  123.  
  124. The order of parameters and multivalued entries, as returned by CGI, is
  125. maintained in the XML.
  126.  
  127. The options hash can be used to customize the behavior a bit more:
  128.  
  129. =over 4
  130.  
  131. =item include => [ list ]
  132.  
  133. Only include the parameters and their values specified in the given list;
  134. all other parameters are not included.  Note that this does not affect the
  135. CGI object storage.
  136.  
  137. =item exclude => [ list ]
  138.  
  139. Do not include the parameters and their values specified in the given list;
  140. all other parameters are included.  Note that this does not affect the
  141. CGI object storage.
  142.  
  143. =back
  144.  
  145. =head1 EXPORT
  146.  
  147. No functions are exported by default, but CGItoXML can be exported by
  148. the user.
  149.  
  150. =head1 AUTHOR
  151.  
  152. Michael K. Neylon, E<lt>mneylon-pm@masemware.comE<gt>
  153.  
  154. =head1 SEE ALSO
  155.  
  156. L<perl>, L<CGI::XML>, L<XML::Simple>.
  157.  
  158. =cut
  159.