All pastes #446054 Raw Edit

Untitled

public text v1 · immutable
·published 2007-04-18 08:11 UTC
rendered paste body
<?php 
include("dbconnect.php");

$file = "prnews/sample.xml"; // Location of the file where the company uploads using ftp...

////////////////////////////////////////////////////////////////////////////
// A global variable used so that not all elements would be printed since you've asked for only: 
// Head Line, Distributor, Location, Date, Abstract, Texte
$flag = 0;

////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////
//   Initialized the following which will be used to as inputs for MySQL   //
$hea = 1;
$headline = '';

$dis = 1;
$distributor = '';

$loc = 1;
$location = '';

$dat = 1;
$date = '';

$abs = 1;
$abstract = '';

$tex = 1;
$text = '';
///////////////////////////////////////////////////////////////////////////

$xml_parser = xml_parser_create(); 

xml_set_element_handler($xml_parser, "startTag", "endTag"); 

xml_set_character_data_handler($xml_parser, "contents"); 

$fp = fopen($file, "r"); 

$data = fread($fp, 80000); 

if(!(xml_parse($xml_parser, $data, feof($fp)))){ 
    die("Error on line " . xml_get_current_line_number($xml_parser)); 
} 

xml_parser_free($xml_parser); 

fclose($fp); 

//////////////////////////////////////////////////////////////

//   Functions for parsing   //

function contents($parser, $data){ 
	
	if ( $GLOBALS['flag'] == 1 )
	{
		if ( $GLOBALS['hea'] == 1 )
		{
			echo $data;
			$GLOBALS['headline'] .= $data; 
		}
		else if ( $GLOBALS['dis'] == 1 )
		{
			echo $data;
			$GLOBALS['distributor'] .= $data; 
		}
		else if ( $GLOBALS['loc'] == 1 )
		{
			echo $data;
			$GLOBALS['location'] .= $data; 
		}
		else if ( $GLOBALS['dat'] == 1 )
		{
			echo $data;
			$GLOBALS['date'] .= $data; 
		}	
		else if ( $GLOBALS['abs'] == 1 )
		{
			echo $data;
			$GLOBALS['abstract'] .= $data; 
		}
		else if ( $GLOBALS['tex'] == 1 )
		{
			echo $data;
			$GLOBALS['text'] .= $data; 
		}								
	}
}

function startTag($parser, $data){ 
    switch($data)
	{
		case 'HEDLINE':
			echo "HEAD LINE: <br />";
			$GLOBALS['flag'] = 1;
			$GLOBALS['hea'] = 1;
		break;
		case 'DISTRIBUTOR':
			echo "<br /><br />DISTRIBUTOR: <br />";
			$GLOBALS['flag'] = 1;
			$GLOBALS['dis'] = 1;			
		break;		
		case 'LOCATION':
			echo "<br /><br />LOCATION: <br />";
			$GLOBALS['flag'] = 1;
			$GLOBALS['loc'] = 1;			
		break;	
		case 'STORY.DATE':
			echo "<br /><br />DATE: <br />";
			$GLOBALS['flag'] = 1;
			$GLOBALS['dat'] = 1;
		break;
		case 'ABSTRACT':
			echo "<br /><br />ABSTRACT: <br />";		
			$GLOBALS['flag'] = 1;
			$GLOBALS['abs'] = 1;			
		break;
		case 'BLOCK':
			echo "<br /><br />BLOCK: <br />";		
			$GLOBALS['flag'] = 1;
			$GLOBALS['tex'] = 1;			
		break;
		case 'BLOCK':
			echo "<br /><br />BLOCK: <br />";		
			$GLOBALS['flag'] = 1;
			$GLOBALS['tex'] = 1;			
		break;		
	} 
} 

function endTag($parser, $data){ 
	$GLOBALS['flag'] = 0;
	
    switch($data)
	{
		case 'HEDLINE':
			$GLOBALS['hea'] = 0;
		break;
		case 'DISTRIBUTOR':
			$GLOBALS['dis'] = 0;			
		break;		
		case 'LOCATION':
			$GLOBALS['loc'] = 0;			
		break;	
		case 'STORY.DATE':
			$GLOBALS['dat'] = 0;
		break;
		case 'ABSTRACT':
			$GLOBALS['abs'] = 0;			
		break;
		case 'BLOCK':
			$GLOBALS['tex'] = 0;			
		break;
	} 
} 

mysql_query("INSERT INTO parse  ( inc, headline, distributor, location, dat, abstract, texte )
			 			 VALUES ( '', '".$GLOBALS['headline']."', '".$GLOBALS['distributor']."', '".$GLOBALS['location']."', '".$GLOBALS['date']."', '".$GLOBALS['abstract']."', '".$GLOBALS['text']."' ) ") or die(mysql_error());
//////////////////////////////////////////////////////////////
?>