从 Perl CGI 脚本将 XML 输出到浏览器?

发布于 2024-11-28 22:35:31 字数 700 浏览 2 评论 0原文

我有一个在 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 技术交流群。

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

发布评论

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

评论(2

疏忽 2024-12-05 22:35:31

实际上,您可以在不使用 CGI 的情况下做到这一点。

print "Content-Type: text/xml\r\n";   # header tells client you send XML
print "\r\n";                         # empty line is required between headers
                                      #   and body
print $output;                        # the body: XML

You can do it without using CGI actually.

print "Content-Type: text/xml\r\n";   # header tells client you send XML
print "\r\n";                         # empty line is required between headers
                                      #   and body
print $output;                        # the body: XML
栖竹 2024-12-05 22:35:31

CGI 是相对简单的协议,它将脚本的标准输出发送到客户端计算机。您所需要做的就是将标头放在输出的开头。这对我有用:

use CGI qw(:standard);
use XML::Writer;

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();

print header('text/xml'), $output;

还要确保将 shebang 行放在开头并使脚本可执行,以便服务器知道如何运行它:

#!perl

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:

use CGI qw(:standard);
use XML::Writer;

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();

print header('text/xml'), $output;

Also make sure that you put shebang line at the start and make the script executable, so the server know how to run it:

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