Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

XML module which one

by spencerr1 (Novice)
on Feb 05, 2013 at 02:06 UTC ( #1017043=perlquestion: print w/ replies, xml ) Need Help??
spencerr1 has asked for the wisdom of the Perl Monks concerning the following question:

I have a cgi.pm form results in key value order key = value Is there a simple module I can run up against it and get the results in XML. key being the name of the field on the form. value being what was entered on the form. <key>value<key>

Comment on XML module which one
Re: XML module which one
by 7stud (Deacon) on Feb 05, 2013 at 03:56 UTC

    XML::Simple can do it:

    use strict; use warnings; use 5.012; use XML::Simple; my %form_data = ( name => 'Joe', weight => 180, ); my $xml_obj = XML::Simple->new(RootName => 'form1'); say $xml_obj->XMLout( \%form_data, noattr => 1, xmldecl => q{<?xml version="1.0">}, ); --output:-- <?xml version="1.0"> <form1> <name>Joe</name> <weight>180</weight> </form1>

      I was thinking about using XML::Simple because it looks simple enough but when I get data from the form (many name = value results it looks like I would have to manually enter them in another script right? I was hopping to have a script that opened up the cgi.pm form results and could draw the key = value results in an XML document.

        ...but when I get data from the form (many name = value results it looks like I would have to manually enter them in another script right? I was hopping to have a script that opened up the cgi.pm form results and could draw the key = value results in an XML document.

        What do you mean 'another script'? Even if you had to retrieve each name/value pair one by one and insert them in a hash, then feed the hash to XML::Simple to create the xml, you would do it all in the same script. Moreover, you do not have to retrieve the form data one name/value at a time--because the cgi module makes the form data available to you as a hash. See the section titled FETCHING THE PARAMETER LIST AS A HASH in the cgi docs. Then you can pass that hash as an argument to XMLout() to produce the xml.

Re: XML module which one
by tmharish (Friar) on Feb 05, 2013 at 04:10 UTC

    XML::Smart:

    use strict ; use warnings ; use XML::Smart ; my %hash = ( 'key1' => 'value1' , 'key2' => 'value2' , 'key3' => 'value3' , 'key4' => 'value4' , ); my $xml_obj = new XML::Smart(); foreach my $key ( keys %hash ) { $xml_obj->{form}{$key} = { CONTENT => $hash{ $key } } ; } print $xml_obj->data();

    Output

    <?xml version="1.0" encoding="iso-8859-1" ?> <?meta name="GENERATOR" content="XML::Smart/1.74 Perl/5.010001 [linux] +" ?> <form> <key2>value2</key2> <key4>value4</key4> <key1>value1</key1> <key3>value3</key3> </form>
Re: XML module which one
by sundialsvc4 (Monsignor) on Feb 05, 2013 at 04:14 UTC

    Nodding in assent to the foregoing ... it is almost-100% likely that this simple and lightweight module will be exactly what you need.   Definitely you should choose some XML-handling module, and delegate to it the task of preparing the XML-string for delivery.   (Never try to “do it yourself.”   Bad dog... no biscuit!)   Obviously, there are many XML-handling packages available in Perl, but this one should be entirely adequate for anything that CGI could possibly deliver.

      Yes I think you got it. I will give this a try but it looks like it's exactly what I was looking for.

      Thank you folks for the expert responses

Re: XML module which one
by sundialsvc4 (Monsignor) on Feb 05, 2013 at 14:12 UTC

    One thing to watch out for as you do this is ... multiple responses to the same CGI-variable.   These appear as multiple occurrences in the data-stream, e.g.:

    var1 = 'foo'
    var1 = 'bar'

    Make sure to use a CGI-variable parsing routine that handles that for you, and be sure also to put it to the test.   Multiple selections from a drop-down list can do this, or poorly-written HTML.   Be sure that your code, and your test suite, represents and correctly handles all cases that could exist.

    (Mind you ... test it all the way up-and-down the line, including whatever logic is ultimately going to consume that XML file.)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1017043]
Approved by muba
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (14)
As of 2014-07-30 20:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (240 votes), past polls