如何使用perl生成新的xml文件

发布于 2024-12-15 21:03:00 字数 2351 浏览 0 评论 0 原文

我正在使用 perl 脚本生成新的 xml 文件,但我有一个小问题,我编写了这样的脚本,

 #!/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

但运行此脚本后它没有给出任何错误,它生成 xml 文件,当我尝试打开该 xml 文件时,

 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...

我没有知道是什么错误了,怎么打开。 perl 脚本中是否有任何错误。否则如何使用 perl 生成新的 xml 文件。(xml 数据如下所示),是否有任何选项。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜灵血窟げ 2024-12-22 21:03:00

考虑使用模块来编写 XML,请参阅如何从 Perl 创建 XML?< /a>.

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

梦里南柯 2024-12-22 21:03:00

我刚刚运行了你的脚本,它不能按原样工作。您的脚本试图理解您以字符串形式提供的 XML,因为您的 print 语句正在将 "< 写入文件。通过更改

print $file (<<EOF);

print $file <<EOF

以及 EOF 行,

  EOF

EOF
;

将获得一个将 XML 写入文件的脚本。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文