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
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