2015-06-10 52 views
1

我想用腳本更新我的local.xml。我想用我的自定義腳本在xml中更改我的用戶名,數據庫名稱和密碼。我正在創建腳本。它可以顯示該節點的值,並更新值,但在節點 代碼不能與值設置CDATA: -使用magento中的腳本更新local.xml

$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'); 

我想與我的價值觀在XML中添加CDATA。像這樣

<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> 

請給我建議,我該怎麼辦?

回答

1

試試下面的代碼,並改變任何你想要的。

<?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'); 
?> 

注意:您可以像對象和數組一樣迭代獲得任何節點。

+0

它的工作,但不能添加CDATA。 你的代碼後,顯示 your_host_name 我想這 <![CDATA [your_host_name <]]> 請建議 –

+0

我想與CDATA –

+0

$ XMLFILE =的file_get_contents值( 'node.xml')!; $ xmlNodes = new SimpleXMLExtended($ xmlFile); $ xmlNodes-> host = NULL; $ node = dom_import_simplexml($ xmlNodes-> host); $ no = $ node-> ownerDocument; $ node-> appendChild($ no-> createCDATASection('your host name')); $ xmlNodes-> asXML('node.xml'); – Jegan

0

這是最終的代碼。其工作正常

$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'; 
    } 
相關問題