0

I am generating new xml file using perl script but I have small problem, I written script like this

 #!/usr/bin/perl
 use warnings; 
  use strict;
open my $file, '>>', 'data.xml' or die "Can't open file: $!";
 print $file (<<EOF);
<?xml version="1.0" encoding="UTF-8"?>
<Detailsofuniversitys xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <Identification>
  <Ind>03.20.00</Ind>
</Identification>
<MetaData>
<History>
 <Entry>
  <Date>2010-10-19</Date>
<Note>IND Created</Note>
</Entry>
</History>
 <Status>P</Status>
 <StoringAndErasing>Not Defined</StoringAndErasing>
 <StoringAndErasing>Not Defined</StoringAndErasing>
 </MetaData>
 <university>
 <name>svu</name>
 <location>ravru</location>
<branch>
<electronics>
 <student name="xxx" number="12">
 <semester number="1"subjects="7" rank="2"/>
 </student>
 <student name="xxx" number="15">
 <semester number="1" subjects="7" rank="10"/>
 <semester number="2" subjects="4" rank="1"/>
  </student>
   <student name="xxx" number="16">
   <semester number="1"subjects="7" rank="2"/>
  <semester number="2"subjects="4" rank="2"/>
   </student>
</electronics>
  </branch>
</university>
<university>
  <name>sku</name>
    <location>ANTP</location>
<branch>
 <electronics>
   <student name="xxx" number="12">
 <semester number="3"subjects="6" rank="20"/>
 </student>
   <student name="xxx" number="16">
   <semester number="1"subjects="9" rank="12"/>
  <semester number="2"subjects="4" rank="2"/>
   </student>
    </section>
</electronics>
 </branch>
</university>
 EOF

But after running this script It doesn't give any errors, it genarating xml file , when I tried to open that xml file

 The XML page cannot be displayed 
  Cannot view XML input using XSL style sheet. Please correct the error and then click   
  the Refresh button, or try again later. 


   The following tags were not closed: gpx, gpx, detailsofuniversitys. Error processing 
     resource 'file:///C:/files/star/data...

I don't know what is the error is this, how to open. Is there any error in perl script.otherwise how can I generate new xml file using perl.(xml data look like this), is there any option.

Cœur
  • 37,241
  • 25
  • 195
  • 267
biji
  • 59
  • 7
  • Cut this down to a tiny xml output and try again. What are you using to open the file? – KevinDTimm Nov 15 '11 at 17:13
  • possible duplicate of [How can I create XML from Perl?](http://stackoverflow.com/questions/154762/how-can-i-create-xml-from-perl) – daxim Nov 15 '11 at 18:19
  • The string that you are writing into the file is not valid XML. The tag is opened, but not closed. This problem has nothing to do with Perl. The problem is with your document. – Dave Cross Nov 15 '11 at 20:48
  • Thank you very much for every one, who help to solve my problem, Now its working, Its totally my fault, My XML data is not well formatted.Sorry for posting this type of question, because I am beginner. – biji Nov 16 '11 at 08:01

2 Answers2

1

Consider using a module for writing XML, see How can I create XML from Perl?.

Community
  • 1
  • 1
SAN
  • 2,219
  • 4
  • 26
  • 32
0

I just ran you script, it doesn't work as is. Your script is trying to make sense of the XML that you have giving it as a string literal because your print statement is writing "<<EOF" to the file. By changing

print $file (<<EOF);

to

print $file <<EOF

and the EOF line from

  EOF

to

EOF
;

you get a script that writes XML to a file.

zellio
  • 31,308
  • 1
  • 42
  • 61
  • I tried like as you said but it giving error like this "can't find string terminator "EOF" anywhere before EOF at line 5" in my script.I can't able to modify where I missed string. – biji Nov 15 '11 at 17:26
  • You need to make sure that EOF is on it's own line and doesn't have any leading white space ( it needs to be flush with the edge of the page ) – zellio Nov 15 '11 at 17:30
  • 1
    With heredocs, it's better practice to put the `;` sooner rather than later. See http://stackoverflow.com/q/4015984/133939 – Zaid Nov 15 '11 at 17:35
  • no, I cant open the xml file it giving same error as described in question. perl script executing without errors but when I tried to open with xml file can't open with internet explorer. – biji Nov 15 '11 at 17:58
  • @biji what is this line? close your xml tags properly – SAN Nov 15 '11 at 18:44
  • Thank you very much for every one, who help to solve my problem, Now its working, Its totally my fault, My XML data is not well formatted.Sorry for posting this type of question, because I am beginner. – biji Nov 16 '11 at 08:02