简体   繁体   English

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

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

This is my XML file as conf.xml : 这是我的XML文件,如conf.xml

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

This is my PHP file as change.php 这是我的PHP文件, change.php

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

I want that variable $DBUNAME and $DBPWD gets added into the XML file as an element 我希望将变量$DBUNAME$DBPWD作为元素添加到XML文件中

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

This will work for your need, 这将满足您的需求,

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