How to Write and Read XML in PHP

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


Related Topic Brief Overview of File, Directory Level Permission Set-up and Permission Assignment
Javascript Date Object Constructor with Examples
What are All the Features Available in Android to Develop Application
List of Mysql Error Code and Description
Basic Linux Commands

nScraps.com 2011   Privacy Policy  Terms of Service  Feedback