如何将PHP变量值存储到XML文件中?

时间:2013-09-18 04:56:37

标签: php xml

这是我的XML文件conf.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<conf>
  <auth>
     <ids></ids>
     <key></key>
  </auth>
</conf>

这是我的PHP文件change.php

<?php   
  $DBUNAME="BOB1";
  $DBPWD="BOB2";  
?>

我希望变量$DBUNAME$DBPWD作为元素添加到XML文件中

<ids>BOB1</ids>
<key>BOB2</key>

1 个答案:

答案 0 :(得分:1)

这将满足您的需求,

<?php 

$file ="config.xml";

$DBUNAME="BOB1";
$DBPWD="BOB2";

//load xml object
$xml= simplexml_load_file($file);

//assign auth id
$xml->auth->ids = $DBUNAME;

//assign auth key
$xml->auth->key = $DBPWD;

//store the value into the file
file_put_contents($file, $xml->asXML());

?>