Search code examples
phpxmlsimplexmlcdata

How to write CDATA using SimpleXmlElement?


I have this code to create and update xml file:

<?php
$xmlFile    = 'config.xml';
$xml        = new SimpleXmlElement('<site/>');
$xml->title = 'Site Title';
$xml->title->addAttribute('lang', 'en');
$xml->saveXML($xmlFile);
?>

This generates the following xml file:

<?xml version="1.0"?>
<site>
  <title lang="en">Site Title</title>
</site>

The question is: is there a way to add CDATA with this method/technique to create xml code below?

<?xml version="1.0"?>
<site>
  <title lang="en"><![CDATA[Site Title]]></title>
</site>

Solution

  • Got it! I adapted the code from this great solution (archived version):

        <?php
        
        // http://coffeerings.posterous.com/php-simplexml-and-cdata
        // https://web.archive.org/web/20110223233311/http://coffeerings.posterous.com/php-simplexml-and-cdata
        
        // Customized 'SimpleXMLElement' class.
        class SimpleXMLExtended extends SimpleXMLElement {
        
          // Create CDATA section custom function.
          public function addCData( $cdata_text ) {
            $node              = dom_import_simplexml( $this ); 
            $ownerDocumentNode = $node->ownerDocument;
            
            $node->appendChild( $ownerDocumentNode->createCDATASection( $cdata_text )); 
          }
        
        }
        
        // How to create the following example, below:
        // <?xml version="1.0"?>
        // <site>
        //   <title lang="en"><![CDATA[Site Title]]></title>
        // </site>
        
        /*
         * Instead of SimpleXMLElement:
         * $xml = new SimpleXMLElement( '<site/>' );
         * create from custom class, in this case, SimpleXMLExtended.
        */
        
        // Name of the XML file.
        $xmlFile    = 'config.xml';
    
        // <?xml version="1.0"?>
        // <site></site>
        // ^^^^^^^^^^^^^
        $xml        = new SimpleXMLExtended( '<site/>' );
        
        // Insert '<title><title>' into '<site></site>'.
        // <?xml version="1.0"?>
        // <site>
        //   <title></title>
        //   ^^^^^^^^^^^^^^^
        // </site>
        $xml->title = NULL; // VERY IMPORTANT! We need a node where to append.
        
        // CDATA section custom function.
        // <?xml version="1.0"?>
        // <site></site>
        // <title><![CDATA[Site Title]]></title>
        //        ^^^^^^^^^^^^^^^^^^^^^^
        // </site>
        $xml->title->addCData( 'Site Title' );
        
        // Add an attribute.
        // <?xml version="1.0"?>
        // <site></site>
        //   <title lang="en"><![CDATA[Site Title]]></title>
        //          ^^^^^^^^^^
        // </site>
        $xml->title->addAttribute( 'lang', 'en' );
        
        // Save.
        $xml->saveXML( $xmlFile );
        
        ?>
    

    XML file, config.xml, generated:

        <?xml version="1.0"?>
        <site>
          <title lang="en"><![CDATA[Site Title]]></title>
        </site>
    

    Thank you Petah, hope it helps!