Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6000011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:37:58+00:00 2026-05-23T00:37:58+00:00

I have this code to create and update xml file: <?php $xmlFile = ‘config.xml’;

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:37:59+00:00Added an answer on May 23, 2026 at 12:37 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using PHP5 to create XML files. I have code like this: $doc =
I have this PHP code that adds a set of nodes to an XML
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have a python script the runs this code: strpath = sudo svnadmin create
I have made a SSIS package to create an XML file, which works fine
I have created this code, and when I run it, don't get any errors
I have this code in jQuery, that I want to reimplement with the prototype
I have this code: chars = #some list try: indx = chars.index(chars) except ValueError:
I have this code that performs an ajax call and loads the results into
I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.