【发布时间】:2016-10-26 17:09:15
【问题描述】:
我有一个 XML 文件和一个 HTML 文件,并且想将它们合并到一个新文档中。
对我来说这也是第一次做这样的事情,因为我不是 PHP 开发人员。
这样的结果应该变成word文档了……
XML 文件
会生成这个文件(来源:drupal)
<professie>Manager</professie>
<gebdate>1960</gebdate>
<project>
<rol>Projectmanager</rol>
<opdrachtgever>Apple</opdrachtgever>
<result>Lorum ipsum Lorum ipsumLorum ipsum Lorum ipsum Lorum ipsum Lorum ipsumLorum ipsum Lorum ipsum Lorum ipsum Lorum ipsumLorum ipsum Lorum ipsum </result>
<time>2012-2013</time>
</project>
<project>
<rol>Teamleader</rol>
<opdrachtgever>Google</opdrachtgever>
<result>Lorum at google ipsum Lorum ipsumLorum ipsum Lorum ipsum Lorum ipsum Lorum ipsumLorum ipsum Lorum ipsum Lorum ipsum Lorum ipsumLorum ipsum Lorum ipsum </result>
<time>2011-2014</time>
</project>
包含内联 css 的 HTML 模板文件
实际上,这将是一个导出的 word 文档,格式为 html 过滤的文件
<table style="width: 100%;">
<tbody>
<tr>
<td>Profession</td>
<td>[professie]</td>
</tr>
<tr>
<td>Date of birth</td>
<td>[gebdate]</td>
</tr>
</tbody>
</table>
<project>
<table style="width: 100%;">
<tbody>
<tr>
<td>></td>
<td>Rol</td>
<td>:</td>
<td>[rol]</td>
</tr>
<tr>
<td> </td>
<td>Opdrachtgever</td>
<td>:</td>
<td>[opdrachtgever]</td>
</tr>
<tr>
<td></td>
<td>Resultaat</td>
<td>:</td>
<td>[result]</td>
</tr>
<tr>
<td></td>
<td>Datum</td>
<td>:</td>
<td>[time]</td>
</tr>
</tbody>
</table>
所以我构建了一个函数来执行此操作。目前我构建这样的伪代码:
<?php
function generatemydocument ($path_to_content_file,$path_to_template_file,$path_to_output_file){
if (!file_exists($path_to_xml_file)){
return;
}
if (!file_exists($path_to_template_file)){
return;
}
if (file_exists($path_to_output_file)){
// make log message output file already exist
return;
}
// read the file into a string
$templatefile = readfileascompletestring ($path_to_template_file)
// Search and replace all single dom elemelens with no children
// Get all elements from dom document with no childs and put it in a array
// Reallly nu clu how to do this yet ...
$array_with_strings_to_replace = array("all elements from $path_to_content_file with NO children","all values for this element")
// init
$dbData = array();
foreach ($array_with_strings_to_replace as $key => $value)
$sanitizedValue = strip_tags(ucfirst(strtolower($value)));
$templatefile = str_replace('{$'.$key.'}', $sanitizedValue, $templatefile);
$dbData[$key] = mysql_real_escape_string($sanitizedValue);
// add a line of code into a log file
}
// Search and replace all nodes with the use of a dom translation
$content = new DOMDocument();
$content->loadXML($path_to_content_file);
$template = new DOMDocument();
$template ->loadHTML($templatefile);
// Create a new document
$newdoc = new DOMDocument;
$newdoc->formatOutput = true;
// Set the template in the newdoc
$newdoc = $template
// Import the node, and all its children, to the document
$node = $newdoc->importNode($node, true);
// I think I have to do some replacements here but I'm a little bit lost in here
// And then append it to the "<root>" node
$newdoc->documentElement->appendChild($node);
$newdoc->saveHTML($path_to_output_file );
}
?>
但是我对这个php的dom库了解的越多,我就有一些疑问。
1) 现在,我对没有子节点的节点执行字符串替换操作。我觉得这可以用 DOMNode DOMDocument::importNode 来完成,并且 importnode 只是导入带有或不带有子节点的节点。对吗?
2) 我不明白如何处理内容和模板中节点上的合并。我也应该替换并搜索它吗?
3) 我不认为这是这个世界上的新事物。是否有一个库函数可以做到这一点?
该函数不应包含来自 dom 文档本身的任何信息,我可以使用我们需要合并这个完整事物的信息来更改函数的输入变量,例如包含所有元素名称的列表。
这可以更容易地完成吗?
最后这应该是drupal7中的一个模块,用户在系统中选择一个节点(这将给出content.xml),这个节点将被下载到一个word文档中(template.html是这个的基础)。
【问题讨论】:
标签: php html xml drupal ms-word