update local.xml with script in magento

391 Views Asked by At

I want to update my local.xml with script. I want to change my username , database name and password in the xml using my custom script. I am creating script. it can show the values of the node and also update the values but it can't set CDATA with the values in the node code:-

$xml = simplexml_load_file('app/etc/local.xml'); // connection details are inside of CDATA
$conn = $xml->global->resources->default_setup->connection;
echo $user =  $conn->username;
 $pass =  $conn->password;
 $pass =  $conn->dbname;
// create CDATA section

echo $conn->username = '<'.'![CDATA['.'user'.']]'.'>';
echo $conn->password = '<'.'![CDATA['.'pass'.']]'.'>';
echo $conn->dbname = '<'.'![CDATA['.'bdname'.']]'.'>';


$xml->asXML('app/etc/local.xml');

I want to add CDATA with my values in the xml. like this

<connection>
                    <host><![CDATA[localhost]]></host>
                    <username><![CDATA[root]]></username>
                    <password><![CDATA[ghrix321]]></password>
                    <dbname><![CDATA[bachatbay]]></dbname>
                    <initStatements><![CDATA[SET NAMES utf8]]></initStatements>
                    <model><![CDATA[mysql4]]></model>
                    <type><![CDATA[pdo_mysql]]></type>
                    <pdoType><![CDATA[]]></pdoType>
                    <active>1</active>
                </connection>

Please suggest me , what can i do ?

2

There are 2 best solutions below

7
On BEST ANSWER

Try this below code, and alter whatever you want.

<?php
    $xmlFile = file_get_contents('Magen/app/etc/local.xml');// File you want to alter
    $xmlNodes = new SimpleXMLExtended($xmlFile);
    $xmlNodes->global->resources->default_setup->connection->dbname  = NULL;
    $domNode = dom_import_simplexml($xmlNodes->global->resources->default_setup->connection->dbname);
    $ownerNode   = $domNode->ownerDocument;
    $domNode->appendChild($ownerNode->createCDATASection('your_host_name')); //values you want to change
    $xmlNodes->asXML('Magen/app/etc/local.xml'); 
?>

Note: You can get any nodes by iterating like object and arrays.

0
On

this is the final code. its working fine

$xmlFile = file_get_contents('app/etc/local.xml');
    $xmlNodes = new SimpleXMLElement($xmlFile);

    $xmlNodes->global->resources->default_setup->connection->dbname = NULL;
    $node = dom_import_simplexml($xmlNodes->global->resources->default_setup->connection->dbname);
    $no = $node->ownerDocument;
    $node->appendChild($no->createCDATASection($databasename));

    $xmlNodes->global->resources->default_setup->connection->username = NULL;
    $node = dom_import_simplexml($xmlNodes->global->resources->default_setup->connection->username);
    $no = $node->ownerDocument;
    $node->appendChild($no->createCDATASection($username));

    $xmlNodes->global->resources->default_setup->connection->password = NULL;
    $node = dom_import_simplexml($xmlNodes->global->resources->default_setup->connection->password);
    $no = $node->ownerDocument;
    $node->appendChild($no->createCDATASection($password));
    if($xmlNodes->asXML('app/etc/local.xml')){
        echo 'your local.xml is updated';   
    }else{
        echo 'Your local.xml is not updated';
    }