XML Read and Write in PHP Programming
The php function to Read and Write XML file is used where the XML files are need to be dynamically read and write into a file. The XML textual data well supported by unicode for the languages focuses on documents can be Read and Write easily by the function given below
PHP Code to Read and Write XML Files
<?php
session_start();
$_SESSION['arrConfig'];
$docConfigXML = new DOMDocument;
$docConfigXML->preserveWhiteSpace = FALSE;
$docConfigXML->load( 'resources/xml/config.xml' );
$_SESSION['arrConfig']['fax'] = trim($docConfigXML->getElementsByTagName("fax")->item(0)->getAttribute('value'));
$_SESSION['arrConfig']['phone'] = trim($docConfigXML->getElementsByTagName("phone")->item(0)->nodeValue);
echo $_SESSION['arrConfig']['fax'];
echo "<br />";
echo $_SESSION['arrConfig']['phone'];
$strConfigXML = "";
$fax = "702-555-1faxxx";
$phone = '702-555-phone';
$strConfigXML =
<<<END
<?xml version="1.0" encoding="utf-8"?>
<config>
<fax value='$fax' />
<phone>
$phone
</phone>
</config>
END;
$file = "resources/xml/config.xml";
writeFile($file,$strConfigXML);
function writeFile($file,$strFileContent)
{
$fh = fopen($file, 'w' ) or die ("can't create file");
fwrite($fh, $strFileContent);
fclose($fh);
}
?>