How to write CDATA using SimpleXmlElement?

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

I have this code to create and update xml file:

\');
$xml->title          


        
5条回答
  •  渐次进展
    2020-11-27 15:54

    You can also create a helper function for this, if you'd rather not extend SimpleXMLElement:

     /**
      * Adds a CDATA property to an XML document.
      *
      * @param string $name
      *   Name of property that should contain CDATA.
      * @param string $value
      *   Value that should be inserted into a CDATA child.
      * @param object $parent
      *   Element that the CDATA child should be attached too.
      */
     $add_cdata = function($name, $value, &$parent) {
       $child = $parent->addChild($name);
    
       if ($child !== NULL) {
         $child_node = dom_import_simplexml($child);
         $child_owner = $child_node->ownerDocument;
         $child_node->appendChild($child_owner->createCDATASection($value));
       }
    
       return $child;
     };
    

提交回复
热议问题