-3

I tried many ways to create a XML file but doesn't work. Below is the code to generate plain text file. Can someone suggest a method on how I can create a XML file instead of a text file in the below code?

#!/usr/bin/perl -w
use CGI;
use strict;
use warnings;
#use CGI::Carp qw(fatalsToBrowser);

my $q = new CGI;

print $q->header;
print $q->start_html(-title => "Line Calculation");
my $namex1 = $q->param('value_x1');
my $namex2 = $q->param('value_x2');
my $namey1 = $q->param('value_y1');
my $namey2 = $q->param('value_y2');
#entered values are printed:
print $q->h1("Calculation of slope and y-intercept");
print $q->ul(
$q->li(["<b>X1 value:</b> $namex1","<b>X2 value:</b> $namex2",
"<b>Y1 value:</b> $namey1", "<b>Y2 value:</b> $namey2"])
);

#calculation of slope and y-intercept is below:
my $slope1 = ($namey2-$namey1)/($namex2-$namex1);
print $q->h2("Slope(m) is $slope1"); 
my $inter = ($namey1-($slope1*$namex1));
print $q->h2("y-intercept(b) is $inter"); 

#file is processed below:
my $filename = 'file.txt';
open(my $fh, '>>', $filename) or die "Could not open file '$filename' $!";
print $fh "\n\nx1=$namex1, x2=$namex2, y1=$namey1, y2=$namey2\n";
print $fh "\nslope is $slope1, y-intercept is $inter\n\n";
close $fh;


open (MYFILE, '>data.xml');
print MYFILE qq{<?xml version="1.0" encoding="UTF-8"?>};
print MYFILE '<x1=$namex1, x2=$namex2, y1=$namey1, y2=$namey2>';
print MYFILE '<slope is $slope1, y-intercept is $inter>';
close (MYFILE);

print $q->li("\nall the list of data entered so far is accessible in file: 'file.txt' and 'data.xml'\n");

print $q->end_html;
Grokify
  • 15,092
  • 6
  • 60
  • 81

2 Answers2

0

Here's the bit of your code which creates an XML file.

open (MYFILE, '>data.xml');
print MYFILE qq{<?xml version="1.0" encoding="UTF-8"?>};
print MYFILE '<x1=$namex1, x2=$namex2, y1=$namey1, y2=$namey2>';
print MYFILE '<slope is $slope1, y-intercept is $inter>';
close (MYFILE);

There are a few things I'd change, but you've basically got it working. You open the file, write data to the file and then close the file successfully.

Here's what you'll end up in data.xml (I've replaced all of your variables with XXX as I have no idea what values they will have):

<?xml version="1.0" encoding="UTF-8"?>
<x1=XXX, x2=XXX, y1=XXX, y2=XXX>
<slope is XXX, y-intercept is XXX>

The problem is, that this isn't valid XML. For a start, you need a root element that encloses all other elements in your document. Something like this, perhaps:

<?xml version="1.0" encoding="UTF-8"?>
<data>
  <x1=XXX, x2=XXX, y1=XXX, y2=XXX>
  <slope is XXX, y-intercept is XXX>
</data>

See, how I've added a <data> ... </data> around all of your other elements?

And then the two elements that you've added just don't look anything like valid XML. A valid XML element has a name and optional attributes. Your first element is close. It has attributes but no name. Perhaps it should look like this:

<coords x1=XXX, x2=XXX, y1=XXX, y2=XXX>

(But I've invented the name, coords, only you can know what it needs to be called.)

Oh, and an XML element needs to be closed. The easiest way is to add a "self-closing" slash to the end of the element.

<coords x1=XXX, x2=XXX, y1=XXX, y2=XXX />

Basically (and I'm sorry if this sounds harsh), in order to create an XML file, you need to know what an XML file looks like. And it really doesn't look like you do. Perhaps you need to do some reading on the subject. Or perhaps you need to get more information from the person who asked you to create the XML file. Try asking them for a specification for the format of the XML.

Really, you've got the Perl stuff pretty much right. It's the non-Perl stuff you need help with. And we're not the right people to help you there.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
0

Instead of programming all operations on your own, check packages devoted to XML, like XML::Writer or XML::LibXML.

Even on StackOverflow you can find some examples how to use them.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41