Sanjoy Roy

[MCM, MCP, SCJP] – Senior PHP Programmer

Parse XML Spreadsheet using PHP DOMDocument


xmlparser.html:

File Name:  
Options: Column Heading Included
Column Heading & Name Included
Just Data
 
Hidden Parameters:

 

xmlparser.php:

$fileType 	= isset($_FILES['file']['type']) ? $_FILES['file']['type']: '';
if ($fileType !='' && $fileType == 'text/xml'){
	  $ext = 'xml';
	  if ($_FILES['file']['tmp_name']){
	  	  //Copy to the backup directory
	  	  $filename 	= $user.'.'.$section.'.upload.'.date('Ymd').'.'.$ext;
		  copy($_FILES['file']['tmp_name'], $UPLOAD_CSV_PATH.$filename);
		  //parse xml spredsheet
		  $dom 		= DOMDocument::load($UPLOAD_CSV_PATH.$filename);
		  $rows 	= $dom->getElementsByTagName('Row');
		  $data		= array();

		  foreach ($rows as $row){
			  $index 	= 1;
			  $cells 	= $row->getElementsByTagName('Cell');
			  $tempdata = array();
			  foreach( $cells as $cell ){
				  $ind = $cell->getAttribute('Index');
				  if ( $ind != null ) $index = $ind;
				  //echo $cell->nodeValue."
"; $tempdata[] = $cell->nodeValue; $index += 1; }//inner for loop $data[] = implode(",", $tempdata); }//outer for loop }//outer if (file temp_name compare) }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: