Do you want to produce XML right there from the form without submitting it, or from the back-end script processing the form data upon submissions?
For the first option (generate XML on the client side without submitting the form), you have to use JavaScript.
Since Perl is one of your tags, I assume this is NOT what you want to do and won't expand further.
For the second option (back-end script processing the form data upon submissions), you can use the following code structure.
use XML::Simple;
my %form_fields = process_form_data();
# This depends on what framework you use, e.g. CGI.pm, TT, Embperl, etc...
# Most Perl web frameworks already create a hash for you
# with keys being input element names
my %xml_hash = ();
foreach my $name (keys %form_fields) {
next unless $process_form_field{$name}; # If only some are put into XML
$xml_hash{$name} = $form_fields{name}; # Assuming no multiple values.
}
# Loop over $xml_hash keys and find if any required ones are missing...
my $xml_string = XMLOut({root_element_name => \%form_fields});
It's an approximation (the exact answer is impossible since you didn't clarify any of the details, such as your Perl web framework, desired XML module, sample form data or XML structure); I assumed that a predefined set of form elements whose names are in %process_form_field
hash get translated into XML elements with the same name all stored flatly under a <root_element_name>
parent element, and that your Perl web framework has a method process_form_data()
which translates from form data (QUERY_STRING or POST data) into a hash mapping form's input element names to submitted values (e.g. Embperl has a built-in %fdat
hash that automatically achieves this). I further assume none of the elements submit >1 value for simplicity's sake.