使用Perl组合XML文件

时间:2013-12-04 08:29:05

标签: xml perl

我需要使用perl组合xml文件。我编写了这段代码,并从批处理脚本调用,但这可以使用XML编写器,它可以工作,但它取代了所有<和> XMl中的字符到><我希望它只是将@data数组中的任何内容转储到新的XML文件中。

my $XML_COMBINE_FILE = IO::File->new("> $TARGET_DIR/output.xml");

my $writer = XML::Writer->new(OUTPUT => $XML_COMBINE_FILE);
$writer->startTag("wd:Report_Data" => "?xml version=\'1.0\' encoding=\'UTF-8\'?>");

foreach (@downloaded_file_list) 
{

    open FILE, "< $TARGET_DIR/$_" or die "Cannot open $_ for reading: $!\n";
    my @data = <FILE>;
    close FILE;

    $writer->characters("@data");
}

    $writer->endTag("wd:Report_Data");
    $writer->end();
    $XML_COMBINE_FILE->close();

    print "XML files Combined Successfully";

非常感谢任何帮助!谢谢:))

1 个答案:

答案 0 :(得分:1)

来自文档:

characters($data)

    Add character data to an XML document. All '<', '>', and '&' characters in the $data argument will automatically be escaped using the predefined XML entities:

raw($data)

    Print data completely unquoted and unchecked to the XML document. For example raw('<') will print a literal < character. This necessarily bypasses all well-formedness checking, and is therefore only available in unsafe mode.

所以你可以试试这个,它可能有效:

my $writer = XML::Writer->new(OUTPUT => $XML_COMBINE_FILE, UNSAFE => 1);
...
foreach (@downloaded_file_list) 
{

    local $/=undef;
    open my $fh, "<",  "$TARGET_DIR/$_" or die "Cannot open $_ for reading: $!";
    my @data = <$fh>;
    close($fh);

    $writer->raw(@data);
}