我想用脚本更新我的local.xml。我想使用自定义脚本在xml中更改我的用户名,数据库名称和密码。我正在创建脚本。它可以显示节点的值并更新值但是它不能用节点中的值设置CDATA 代码: -
$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>
请建议我,我该怎么办?
答案 0 :(得分: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');
?>
注意:您可以通过迭代对象和数组来获取任何节点。
答案 1 :(得分: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';
}