如何在 Perl 中将 XML 转换为字符串

发布于 2024-12-16 13:39:01 字数 610 浏览 0 评论 0原文

我需要发送 XML 文件作为电子邮件正文。我想将 XML 转换为简单的字符串,将其放入正文中,然后发送电子邮件。我记得我之前在 C# 中通过将文件作为 PlainText 加载到 RichTextBox 中来做到这一点。

my $xmlfilename;
my $myxml;
# XML TO STRING?
my $email = Email::Simple->create(
header => [
To      => '"AB <[email protected]"',
From    => '"CD" <[email protected]>',
Subject => "TEST",
],
body => $myxml,
);
sendmail($email);

有什么想法吗?

I need to send an XML file as the body of an email. I want to convert the XML to a simple string, put it into the body and then send the email. I remember I did that before in C# by loading the file as PlainText into a RichTextBox.

my $xmlfilename;
my $myxml;
# XML TO STRING?
my $email = Email::Simple->create(
header => [
To      => '"AB <[email protected]"',
From    => '"CD" <[email protected]>',
Subject => "TEST",
],
body => $myxml,
);
sendmail($email);

Any idea?

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

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

发布评论

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

评论(2

书信已泛黄 2024-12-23 13:39:01

如果从文件中读取 XML 数据,那么它将是一个“简单字符串”。我不确定这是什么混乱。

my $xmlfilename;

open my $fh, $xmlfilename or die "Can't open $xmlfilename: $!";

my $myxml = do { local $/; <$fh>; };

my $email = Email::Simple->create(
    header => [
        To      => '"AB <[email protected]"',
        From    => '"CD" <[email protected]>',
        Subject => "TEST",
    ],
    body => $myxml,
);

sendmail($email);

If you read the XML data from the file, then it will be a "simple string". I'm not sure what the confusion is.

my $xmlfilename;

open my $fh, $xmlfilename or die "Can't open $xmlfilename: $!";

my $myxml = do { local $/; <$fh>; };

my $email = Email::Simple->create(
    header => [
        To      => '"AB <[email protected]"',
        From    => '"CD" <[email protected]>',
        Subject => "TEST",
    ],
    body => $myxml,
);

sendmail($email);
阿楠 2024-12-23 13:39:01
my $xmlfilename; 
my $myxml; 
open (FILE, '>', "$xmlfilename") or die "$!";
while (<FILE>){
    $myxml = "$myxml"."$_";  

 }
close (FILE);
my $email = Email::Simple->create( 
header => [ 
    To => '"AB <[email protected]"', 
    From => '"CD" <[email protected]>', 
    Subject => "TEST", 
], 
    body => $myxml, 
); 
    sendmail($email);

或者如果你愿意你可以解析 xml
http://metacpan.org/pod/XML::Parser 模块

my $xmlfilename; 
my $myxml; 
open (FILE, '>', "$xmlfilename") or die "$!";
while (<FILE>){
    $myxml = "$myxml"."$_";  

 }
close (FILE);
my $email = Email::Simple->create( 
header => [ 
    To => '"AB <[email protected]"', 
    From => '"CD" <[email protected]>', 
    Subject => "TEST", 
], 
    body => $myxml, 
); 
    sendmail($email);

or if you want you can parse xml
with http://metacpan.org/pod/XML::Parser module

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