E
E
Eugene2017-03-09 10:38:19
PHP
Eugene, 2017-03-09 10:38:19

Add CDATA to xml?

How to update the section in the offer_text element <![CDATA[ ]]>with the contents of the variable in the first case $text, and add it in the second case?

$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());

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question