<?xml version="1.0"?>
<file>
<filename>RLR7610_2012090200293-1.csv</filename>
<filename>RLR7610_2012090200293-1.pdf</filename>
</file>
function generateXML($_datetodelete, $_filenames){
$xmlFile = new DOMDocument();
$xmlFile->load($this->achive_folder.$_datetodelete.".xml");
$file = $xmlFile->documentElement;
foreach($_filenames as $filename) {
$node_filename = $xmlFile->createElement("filename");
$node_filename->appendChild($xmlFile->createTextNode($filename));
$file->appendData($node_filename);
$xmlFile->saveXML();
}
}
我想在最后一个标记<filename>
之后循环并追加子节点及其文本,但目前它替换所有旧标记<filename>
而不是追加。我该怎么做?
答案 0 :(得分:3)
如果您想在最后将新子项附加到xml文件,请使用以下代码。
<?php
//Load the scores XML file
$xmlFile = new DOMDocument();
$xmlFile -> load('file.xml');
$files = $xmlFile -> getElementsByTagName('file');
$newfile = $xmlFile -> createElement("filename", "RLR7610_2012090200293-1.xml");
$files -> item(0) -> appendChild($newfile);
$xmlFile -> save('file.xml');
// View XML File Content
if (file_exists('file.xml'))
{
$xml = simplexml_load_file('file.xml');
print_r($xml);
} else
exit('Failed to Open');
?>