How in the first case to update the <![CDATA[ ]]> section in the offer_text element to the contents of the $text variable, and in the second to add it?
$xml = simplexml_load_file($_SERVER['DOCUMENT_ROOT'] . '/objects.xml'); $xmlElement = $xml->xpath("//offer[@id=" . $id . "]"); if(!empty($xmlElement)) { $xmlElement[0]->offer_text = $text; } else { $add = $xml->offers->addChild('offer'); $add->addAttribute('id', $id); $addText = $add->addChild('offer_text', $text); } file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/objects.xml', $xml->asXML());