Ok, I have below a script I wrote up to try and do what I need but it doesn't seem to work (I get a lot of errors about the $xml = simpleXMLElments($xml) line.
So basically I have an XML file written like this:
<articleResponses>
<articleResponse>
<writtenBy>Joe Somebody</writtenBy>
<response>Nice job on that article!</response>
</articleResponse>
</articleResponses>
And I want to add in a new <articleResponse> element and children. What I want my below script to do is give me this:
<articleResponses>
<articleResponse>
<writtenBy>Joe Somebody</writtenBy>
<response>Nice job on that article!</response>
</articleResponse>
<articleResponse>
<writtenBy>Becky Miller</writtenBy>
<response>Good job.</response>
</articleResponse>
</articleResponses>
And... here is my code. I hope it somewhat makes sense. Thanks so much for any help.
<?php
$filename = 'testxml.xml';
// open file so I can save it later
$theCommentsFile = fopen($filename, 'r+') or die("can't open file");
//open file in simpleXML
$xml = simplexml_load_file($filename);
$addElement = new SimpleXMLElement($xml);
// Create new <articleResponse></articleResponse> element
$new_response = $addElement->addChild('articleResponse');
// Create Children under the new element
$new_response->addChild('writtenBy', 'Becky Miller');
$new_response->addChild('response', 'Good job.');
// Make new element into XML
$addElement ->asXML();
// Add the new element as a child to the main <articleResponses> element
$xml->addChild($addElement);
$newXML = $xml->asXML();
echo $newXML;
// Write the old and new Elements back to the XML file
fwrite($theCommentsFile, $newXML);
// Close the XML file
fclose($theCommentsFile);
?>