簡體   English   中英

如何將PHP變量值存儲到XML文件中?

[英]How to store a PHP variable value into an XML file?

這是我的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>

這將滿足您的需求,

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

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM