从 Perl CGI 脚本将 XML 输出到浏览器?
我有一个在 Apache 服务器上运行的 perl CGI 脚本。除其他事项外,该脚本应该显示一些根据输入生成的 XML。使用 XML::Writer 模块,我创建了一个包含正确 XML 的字符串标量,但我似乎不知道如何将其发布回浏览器。目前我的功能如下所示:
sub display_xml {
# input variables here
my $output = '';
my $writer = XML::Writer->new(
OUTPUT=>\$output,
DATA_MODE => 1,
DATA_INDENT =>1
};
$writer->xmlDecl('UTF-8');
$writer->startTag('response');
#omitted for brevity
$writer->endTag('response');
$writer->end();
}
任何人都可以帮助我吗?将 $output
打印到 STDOUT 不起作用,并且我在 CGI.pm 中没有看到任何函数来执行此操作(print $cgi->header('text/xml') ;
有效,但我不知道如何打印正文)。
I've got a perl CGI script running on an Apache server. The script is, among other things, supposed to display some XML that is generated from input. Using the XML::Writer module, I've created a string scalar containing the right XML, but I can't seem to figure out how to post it back to the browser. Currently my function looks like this:
sub display_xml {
# input variables here
my $output = '';
my $writer = XML::Writer->new(
OUTPUT=>\$output,
DATA_MODE => 1,
DATA_INDENT =>1
};
$writer->xmlDecl('UTF-8');
$writer->startTag('response');
#omitted for brevity
$writer->endTag('response');
$writer->end();
}
Can anyone help me with this? Printing $output
to STDOUT doesn't work, and I didn't see any functions in CGI.pm to do this (print $cgi->header('text/xml');
works, but I can't figure out how to print the body).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,您可以在不使用 CGI 的情况下做到这一点。
You can do it without using CGI actually.
CGI 是相对简单的协议,它将脚本的标准输出发送到客户端计算机。您所需要做的就是将标头放在输出的开头。这对我有用:
还要确保将 shebang 行放在开头并使脚本可执行,以便服务器知道如何运行它:
CGI is relatively simple protocol, that send standard output of your script to client machine. All you need is to put header at the start of the output. This works for me:
Also make sure that you put shebang line at the start and make the script executable, so the server know how to run it: