简体   繁体   English

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

[英]How to convert XML to string in Perl

I need to send an XML file as the body of an email. 我需要发送XML文件作为电子邮件的正文。 I want to convert the XML to a simple string, put it into the body and then send the email. 我想将XML转换为简单的字符串,将其放入正文中,然后发送电子邮件。 I remember I did that before in C# by loading the file as PlainText into a RichTextBox. 我记得我以前在C#中通过将文件作为PlainText加载到RichTextBox中进行了此操作。

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

Any idea? 任何想法?

If you read the XML data from the file, then it will be a "simple string". 如果您从文件中读取XML数据,则它将是“简单字符串”。 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 <a@b.it"',
        From    => '"CD" <c@d.it>',
        Subject => "TEST",
    ],
    body => $myxml,
);

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

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

or if you want you can parse xml with http://metacpan.org/pod/XML::Parser module 或者,如果您愿意,可以使用http://metacpan.org/pod/XML::Parser模块解析xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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