How to write CDATA using SimpleXmlElement?

后端 未结 5 753
情话喂你
情话喂你 2020-11-27 15:08

I have this code to create and update xml file:

\');
$xml->title          


        
5条回答
  •  再見小時候
    2020-11-27 15:54

    Got it! I adapted the code from this great solution:

    ownerDocument; 
        $node->appendChild($no->createCDATASection($cdata_text)); 
      } 
    }
    
    $xmlFile    = 'config.xml';
    // instead of $xml = new SimpleXMLElement('');
    $xml        = new SimpleXMLExtended('');
    $xml->title = NULL; // VERY IMPORTANT! We need a node where to append
    $xml->title->addCData('Site Title');
    $xml->title->addAttribute('lang', 'en');
    $xml->saveXML($xmlFile);
    ?>
    

    XML file generated:

    
    
      <![CDATA[Site Title]]>
    
    

    Thank you Petah

提交回复
热议问题