16 January 2009

PHP Code to Read/Write XML Data


//=============reading xml request ======================

$HTTP_RAW_POST_DATA = $GLOBALS['HTTP_RAW_POST_DATA'];
//echo $HTTP_RAW_POST_DATA; exit;

$handle=fopen('data.xml','w');
fwrite($handle,$HTTP_RAW_POST_DATA);
fclose($handle);

$xml = simplexml_load_file("request.xml");
$UserName = $xml->attributes()->UserName;
$Password = $xml->attributes()->Password;
$PropertyCode = $xml->attributes()->PropertyCode;
$Function = $xml->Function;

//============writing xml output ==============

//MAKE SURE THAT extension=php_xmlrpc.dll is enabled
//;extension=php_domxml.dll must be commented

ob_start();
$doc = new DomDocument('1.0' , 'UTF-8');
$root = $doc->createElement('Response');
$occ = $doc->createElement('Website');

$occ = $root->appendChild($occ);
$occ->setAttribute('Name', 'name');
$occ->setAttribute('PropertyCode', '12345678');
$root = $doc->appendChild($root);

$child = $doc->createElement('Function');
$child = $root->appendChild($child);
$value = $doc->createTextNode('Function 2');
$value = $child->appendChild($value);

// get completed xml document
header("Content-type: text/xml");
$xml_string = $doc->saveXML();
echo $xml_string;
?>

No comments: