2014-09-04 83 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