2014-09-04 236 views
0

我解析xml文档,我想将xml内容转换为没有任何标签的文字普通内容。Perl xml转换为word文档?

例如: 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); 
+0

虽然你不解析XML,是吗?你只需要阅读它。你需要(1)选择一个XML模块,(2)提取文本,然后(3)决定放置空格和换行符的位置。 – 2014-09-04 07:40:54

回答

0

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

检出this article on perlmonks这将教你如何从XML文件中提取数据并从提取的数据中创建一个Word文件。

+0

非常感谢。 @Chankey Pathak和Richard Huxton – depsai 2014-09-04 11:56:46