简体   繁体   中英

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

This is my XML file as 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   
  $DBUNAME="BOB1";
  $DBPWD="BOB2";  
?>

I want that variable $DBUNAME and $DBPWD gets added into the XML file as an element

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

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM