简体   繁体   中英

How to generate new xml file using perl

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM