Perl xml转换为word文档?

时间:2014-09-04 07:02:15

标签: xml perl docx

我正在解析xml文档,我想将xml内容转换为单词普通内容而不带任何标记。

例如: xml文件:

<title>Introduction</title>
<sec>Welcome to this world</sec> 
<ref>Conclusion</ref>

转换为word:

Introduction
Welcome to this world
Conclusion

请提前感谢任何建议。

我已经尝试过这段代码,但是它会准确地将内容写入xml文件中。

use Win32::OLE;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Word';
$Win32::OLE::Warn = 2; # Throw Errors, I'll catch them
use Cwd;
use File::Basename;
system 'cls';
#use strict;
use List::Uniq ':all';
use Sort::Versions;
my $xmlfile = 'D:\file.xml';
my $cnt = _open_utf8($xmlfile);
my $dir = dirname($xmlfile);
my $basename = basename($xmlfile);
my $wordsave = Win32::OLE->new('Word.Application', 'Quit') or die;
    $wordsave->{Visible} = 1;
    my $doc = $wordsave->Documents->Add();
    my $range = $doc->{Content};
    ### insert some text into the document
    $range->{Text} = $cnt;
    ### read text from the document and print to the console
    my $paras = $doc->Paragraphs;
    foreach my $para (in $paras) {
        print ">> " . $para->Range->{Text};
    }
    ### close the document and the application
    $doc->SaveAs(FileName => 'd:\temp.docx', FileFormat => wdFormatDocument);
    $doc->Close();
    $wordsave->Quit();
_save_utf8($dir.'\\out_'.$basename,$cnt);

1 个答案:

答案 0 :(得分:0)

正如Richard在评论中提到的,你需要使用解析器。您可以尝试XML::XPath

查看this article on perlmonks,它将教您如何从XML文件中提取数据并从提取的数据中创建Word文件。