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

When I use CGI::XML it can create <cgi> </cgi> but when I put word eg "hai" it become <cgi><keywords>hai</keywords></cgi>. How <keywords> tag exist? I don't want <keywords> tag exist. How can i do that? Furthermore, I want this code call input file and the whole text placed between this <cgi> </cgi> tag.Please help me.
#!/usr/bin/perl -w use strict; use CGI::XML; my $q = new CGI::XML; # save CGI variables to XML file my $xml = $q->toXML("cgi"); print $xml;

Replies are listed 'Best First'.
•Re: Problem with CGI::XML
by merlyn (Sage) on Feb 04, 2003 at 19:50 UTC
    The purpose of CGI::XML is to transfer between CGI parameters and an XML representation. Apparently, the DTD being used has a keywords tag for that kind of CGI state.

    If you want precise control over the generated XML, you'll have to roll your own code, not use that module, since that module is about saving CGI state in a particular manner.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Problem with CGI::XML
by Coruscate (Sexton) on Feb 04, 2003 at 21:34 UTC

    I think that the results you are getting is exactly as expected. If you are accessing "script.pl?keywords=hai", CGI::XML (actually XML::CGI as it turns out) is going to write up the XML in such a manner that you can see the argument name and its value. (I think I got that right...)

    If you are only trying to create an XML output of <cgi>hai</cgi> and the such, where parameters passed to CGI have nothing to do with it, check out XML::Simple. Then you could do something like this:

    #!/usr/bin/perl -w use strict; use XML::Simple; my $xml; $xml->{'cgi'} = 'hai'; print XMLout( $xml, keeproot => 1 );

    To make things even cooler, you can place the output of XMLout() to a file and then use the XMLin() function to bring it back in and read from the structure:

    my $xml = XMLin( 'file.txt', keeproot => 1 ); print $xml->{'cgi'};

    See XML::Simple Documentation for more of the juicy details.


          C:\>shutdown -s
          >> Could not shut down computer:
          >> Microsoft is logged in remotely.
        

Re: Problem with CGI::XML
by silent11 (Vicar) on Feb 04, 2003 at 21:46 UTC
    If you are wanting to use the CGI module to produce XML this little script may get you started in the right direction. CGI wasn't made to create XML, but this seems to work fine.
    use strict; use CGI::Pretty qw(book chapter); # for pretty XML my $c = new CGI; print $c->book( {-title=>"Programming Perl", -authors=>" Larry Wall, Tom Christian +sen, Jon Orwant"}, $c->chapter({-number=>1},"An Overview of perl"), $c->chapter({-number=>2},"Bits and Pieces"), $c->chapter({-number=>3},"Unary and Binary Operators"), );

    output:

    <book title="Programming Perl" authors=" Larry Wall, Tom Christiansen, + Jon Orwant"> <chapter number="1"> An Overview of perl </chapter> <chapter number="2"> Bits and Pieces </chapter> <chapter number="3"> Unary and Binary Operators </chapter> </book>



    -Silent11
      I too have thought that CGI.pm does a pretty nifty job of creating XML with it's dynamic tag-to-sub magic, but ... looks like you created a monster! ;)

      jeffa

      perl -MCGI=foo -le "print foo{bar=>baz},qux"