Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTML form to XML

Status
Not open for further replies.

Alexmjw222

Technical User
Mar 24, 2009
2
US
I am having a small issue I have a script that takes form data the enters it in to a XML page to be used later but as the script runs it enters spaces before the pervious entry if any one could help that would be great. BTW I am trying to keep it somewhat simple.

============================================================

#!/usr/bin/perl

use CGI ':standard';

"Content-type: text/html\n\n";

$database="items.xml";
$loc=param('loc');
$categories=param('categories');
$item=param('item');
$itemdes=param('itemdes');
$price=param('price');
$unit=param('unit');
$quantity=param('quantity');

$numcat="9";

if ($categories eq giving_works) {
$numcat="0";
}elsif ($categories eq tools_parts){
$numcat="1";
}elsif ($categories eq antiques){
$numcat="2";
}elsif ($categories eq vechicles){
$numcat="3";
}elsif ($categories eq toys_hobbies){
$numcat="4";
}elsif ($categories eq dishware_pottery){
$numcat="5";
}elsif ($categories eq appliances){
$numcat="6";
}elsif ($categories eq furniture){
$numcat="7";
}elsif ($categories eq jewlery){
$numcat="8";
}elsif ($categories eq other){
$numcat="9";
}

open(DATABASE, "<$database");
@lines=<DATABASE>;
pop(@lines);
close(DATABASE);

open(DATABASE, ">$database");
print(DATABASE "");
close(DATABASE);

open(DATABASE, ">>$database");
print(DATABASE "@lines
<$categories>
<item>$loc$numcat$item</item>
<itemdes>$itemdes</itemdes>
<price>$price</price>
<unit>$unit</unit>
<quantity>$quantity</quantity>
</$categories>
</item>");
close(DATABASE);

exit();
 
Maybe some examples of the current output and your expected output would make your requirements clearer; consider posting them between [ code ] tags for clarity.

Are you missing a print before the "Content-type" line?

Have you considered using a hash for the categories instead of a big if/elsif?

Code:
%numcat = (
    giving_works => 0,
    tools_parts => 1,
    ...
    other => 9,
};
...
<item>$loc$numcat{$categories}$item</item>

Also I see no need to open the DATABASE for output twice; if you just open it once using ">" the subsequent output will be appended (like ">>") anyway.

Annihilannic.
 
>...the script runs it enters spaces before the pervious entry...
To avoid that outcome, replace the part of printing @lines like this.
[tt]
open(DATABASE, ">>$database");
[red]#[/red]print(DATABASE "@lines
[blue]while (@lines) {
print DATABASE shift @lines;
}
print DATABASE "[/blue]<$categories>
<item>$loc$numcat$item</item>
<itemdes>$itemdes</itemdes>
<price>$price</price>
<unit>$unit</unit>
<quantity>$quantity</quantity>
</$categories>[red]");[/red]
[red]#[/red]</item>"); #what is it!?
[/tt]
On the large, I may not agree with the detail and/or the approach, but it does not matter. I concentrate only on the quoted problem.
 
amendment
Have left a parentheses too many or too little.
[tt] </$categories>[highlight]";[/highlight][/tt]

Also if the last </item> is the wrapping element, add it back. In any case use a better name for it so that it doesn't collide with the data unnecessarily.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top