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