Pages

Thursday, 20 March 2014

Read and write from xml file by php

By php it is easy to read from an xml file by php.
The following code is how to read from a xml file:

$doc = new DOMDocument();
$doc->load( 'sample.xml' ); // the file you want to read
 $i = 0;
 
// xml read code
 
  $transaction_ids = $doc->getElementsByTagName( "transaction" );
 
 
  $transactions = $doc->getElementsByTagName( "merchant" );
  $transactions_arr = array();
  foreach( $transactions as $transaction )
    {
      $ids = $transaction->getElementsByTagName( "id" );
      $id = $ids->item(0)->nodeValue;
 
      $names= $transaction->getElementsByTagName( "name" );
      $name= $names->item(0)->nodeValue;
 
      $total_amounts = $transaction->getElementsByTagName( "totalAmount" );
      $total_amount = $total_amounts->item(0)->nodeValue;
     
      $account_names = $transaction->getElementsByTagName( "accountName" );
      $account_name = $account_names->item(0)->nodeValue;
     
      $account_numbers = $transaction->getElementsByTagName( "accountNumber" );
      $account_number = $account_numbers->item(0)->nodeValue;
     
      $transaction_dates = $transaction->getElementsByTagName( "transactionDate" );
      $transaction_date = $transaction_dates->item(0)->nodeValue;
     
      $descriptions = $transaction->getElementsByTagName( "description" );
      $description = $descriptions->item(0)->nodeValue;
     
     
      $transaction_id_arr[$i] = $transaction_ids->item($i)->getAttribute('id');
     
      $transactions_arr[$i]=array('id'=>$id,'name'=>$name,'totalAmount',$total_amount,'accountName'=>$account_name,'accountNumber'=>$account_number,'transactionDate'=>$transaction_date,'description'=>$description);
      $i++;
   
  }

 // xml write code
 
  $doc = new DOMDocument();
  $doc->formatOutput = true;
 
  $c = $doc->createElement( "catalog" );
  $doc->appendChild( $c );
 
  $fname = $doc->createElement( "name" );
  $fname->appendChild( $doc->createTextNode( 'Transaction logs' ) );
  $c->appendChild( $fname );
 
  $creationDate = $doc->createElement( "creationDate" );
  $creationDate->appendChild( $doc->createTextNode( date('Y-m-d') ) );
  $c->appendChild( $fname );
 
  $copyright = $doc->createElement( "copyright" );
  $copyright->appendChild( $doc->createTextNode( 'Copyright (C) clickshopdk.com, All rights reserved.' ) );
  $c->appendChild( $copyright );
 
  $version = $doc->createElement( "version" );
  $version->appendChild( $doc->createTextNode( '1.0' ) );
  $c->appendChild( $version );
 
  $description = $doc->createElement( "description" );
  $description->appendChild( $doc->createTextNode( 'Today transactions details.' ) );
  $c->appendChild( $description );
 

  $j = 0;
 
  foreach( $transactions_arr as $transaction )
  {
      
      $t = $doc->createElement( "transaction" );
        $t->setAttribute('id', $transaction_id_arr[$j]);
        $c->appendChild( $t );
     
      $m = $doc->createElement( "merchant" );
      $t->appendChild( $m );
     
      $id = $doc->createElement( "id" );
      $id->appendChild( $doc->createTextNode( $transaction['id'] ) );
      $m->appendChild( $id );
 
      $name = $doc->createElement( "name" );
      $name->appendChild( $doc->createTextNode( $transaction['name'] ) );
      $m->appendChild( $name );
     
      $totalAmount = $doc->createElement( "totalAmount" );
      $totalAmount->appendChild( $doc->createTextNode( $transaction['totalAmount'] ) );
      $m->appendChild( $totalAmount );
     
      $accountName = $doc->createElement( "accountName" );
      $accountName->appendChild( $doc->createTextNode( $transaction['accountName'] ) );
      $m->appendChild( $accountName );
     
      $accountNumber = $doc->createElement( "accountNumber" );
      $accountNumber->appendChild( $doc->createTextNode( $transaction['accountNumber'] ) );
      $m->appendChild( $accountNumber );
     
      $transactionDate = $doc->createElement( "transactionDate" );
      $transactionDate->appendChild( $doc->createTextNode( $transaction['transactionDate'] ) );
      $m->appendChild( $transactionDate );
     
      $description = $doc->createElement( "description" );
      $description->appendChild( $doc->createTextNode( $transaction['description'] ) );
      $m->appendChild( $description );
 
     
      $j++;
     
  }


//echo $doc->saveXML();
  $doc->save("output.xml");


 you will get the same output xml like sample xml file, because you are reading from sample.xml file.

the sample.xml file is here. download sample.xml




Wednesday, 19 March 2014

an error occurred while processing this directive

In WordPress website or any other site you may get like the following error when you open the site:

 [an error occurred while processing this directive]

This may be happen for your root folder directory permission

Root folder directory permission needs to be 755

If the folder permission is 777 it may cause this error.

I have solved one of my problem by changing the folder perssion from 777 to 755.