Reputation: 1
<?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();
}
}
I want to loop and append child node after last tag <filename>
with its text but currently it replace all of old tag <filename>
instead of append. How would I do this?
Upvotes: 0
Views: 383
Reputation: 246
If you want to append new child to your xml file at the end please use below code.
<?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');
?>
Upvotes: 3