我正在尝试提出一个解决方案,允许用户上传启用邮件合并的 Word DOCX 模板文件。理想情况下,系统将读取 DOCX 文件,提取 XML,找到邮件合并字段并将它们保存到数据库中以供将来映射。我可能会使用诸如 Zend LiveDocX 或 PHPDOCX 之类的 SOAP 服务或完全使用其他东西——但现在我需要弄清楚如何识别 DOCX 文件中的字段。为此,我从这篇文章开始: http: //dfmaxwell.wordpress.com/2012/02/24/using-php-to-process-a-word-document-mail-merge/
我已经根据我的需要对其进行了一些调整(这可能是一个问题,尽管我在原始代码中也遇到了同样的错误。)具体来说,我现在没有使用它来执行邮件合并,我只是想要来识别字段。这是我所拥有的:
$newFile = '/var/www/mysite.com/public_html/template.docx';
$zip = new ZipArchive();
if( $zip->open( $newFile, ZIPARCHIVE::CHECKCONS ) !== TRUE ) { echo 'failed to open template'; exit; }
$file = 'word/document.xml';
$data = $zip->getFromName( $file );
$zip->close();
$doc = new DOMDocument();
$doc->loadXML( $data );
$wts = $doc->getElementsByTagNameNS('http://schemas.openxmlformats.org/wordprocessingml/2006/main', 'fldChar');
$mergefields = array();
function getMailMerge(&$wts, $index) {
$loop = true;
$counter = $index;
$startfield = false;
while ($loop) {
if ($wts->item($counter)->attributes->item(0)->nodeName == 'w:fldCharType') {
$nodeName = '';
$nodeValue = '';
switch ($wts->item($counter)->attributes->item(0)->nodeValue) {
case 'begin':
if ($startfield) {
$counter = getMailMerge($wts, $counter);
}
$startfield = true;
if ($wts->item($counter)->parentNode->nextSibling) {
$nodeName = $wts->item($counter)->parentNode->nextSibling->childNodes->item(1)->nodeName;
$nodeValue = $wts->item($counter)->parentNode->nextSibling->childNodes->item(1)->nodeValue;
}
else {
// No sibling
// check next node
$nodeName = $wts->item($counter + 1)->parentNode->previousSibling->childNodes->item(1)->nodeName;
$nodeValue = $wts->item($counter + 1)->parentNode->previousSibling->childNodes->item(1)->nodeValue;
}
if (substr($nodeValue, 0, 11) == ' MERGEFIELD') {
$mergefields[] = strtolower(str_replace('"', '', trim(substr($nodeValue, 12))));
}
$counter++;
break;
case 'separate':
$counter++;
break;
case 'end':
if ($startfield) {
$startfield = false;
}
$loop = false;
}
}
}
return $counter;
}
for ($x = 0; $x < $wts->length; $x++) {
if ($wts->item($x)->attributes->item(0)->nodeName == 'w:fldCharType' && $wts->item($x)->attributes->item(0)->nodeValue == 'begin') {
$newcount = getMailMerge($wts, $x);
$x = $newcount;
}
}
我用 ZipArchive() 打开 DOCX 文件没有问题,如果我使用 print_r($doc->saveHTML()); 我看到 XML 数据很好。问题是,当我执行我的代码时,我得到了致命错误:在指向此的非对象上调用成员函数 item():
$nodeName = $wts->item($counter)->parentNode->nextSibling->childNodes->item(1)->nodeName;
谷歌在试图找出这个错误时让我失望了,谁能指出我正确的方向?提前致谢!