PHP script - MySql to XML converter - Holyguard.net - Web coding and development

Main Menu




browser lang:en

Users Area

Client Login

today cache size is:223155

 

Holyguard rss
rss 2.0 for all sections

 

Portfolio

Some of my projects:
CTIBA
MA-NO Web Agency
BluPool
L'Pratone
Travel in Hotel
Yacht Elements
AbruzzoWeb
Il Mastino
CSI Teramo
Innovazione S.p.a.




Commenti

Aiuto
04/09/2010 by ProgXalpha

19/08/2010 by
ciao jackall
17/07/2010 by marco
aiuto
17/07/2010 by marco
RE: Domanda
08/05/2010 by holyguard
Domanda
05/05/2010 by pryons
Ringraziamento
15/04/2010 by Yuriy, webmaster
edu
12/04/2010 by eduala@libero.it

Leggi tutto »


RAPIDQ

Rapid-Q:The lost files
My old library where i have collected all the rapidq scripts and italian help


Today my pagerank is: Free Page Rank Tool

PHP script - MySql to XML converter

Questo piccolo script serve per convertire un intero Database MySql in una serie di files XML.

In pratica viene generato un file xml per ogni tabella corrispondente nel Database, ad esempio nel caso avessimo una tabella "amici" con campi "id" e "titolo", la struttura dell'xml generato sarebbe questa:



<?xml version="1.0"?>
 <amici>
  <record>
   <id>1</id>
   <titolo><![CDATA[ Pippo ]]></titolo>
  </record>
  <record>
   <id>2</id>
   <titolo><![CDATA[ Pluto ]]></titolo>
  </record>
  ....
 </amici>        


E la tabella verrà salvata in un file dal nome amici_xml.xml (naturalmente potete cambiarlo a vostro piacimento)

Lo script inserisce anche il CDATA quando incontra i campi di tipo STRING e BLOB.
Questa è una versione molto basica, per cui se qualcuno è interessato a migliorie sono disponibile a lavorarci insieme.


	$hostname = "hostname";
	$database = "database";
	$username = "username";
	$password = "password";
	
	// Directory where we write the xml files
	$directory = "/xml/";

	$cnConnection = mysql_pconnect($hostname, $username, $password);
	
	//Conn control
	if (!$cnConnection) {
		print 'Could not connect to mysql';
		exit;
	}
	
	//Read all tables
	$result = mysql_list_tables($database);
	
	if (!$result) {
		print "DB Error, could not list tables\n";
		print 'MySQL Error: ' . mysql_error();
		exit;
	}
	
	//Start print all tables
	while ($row = mysql_fetch_row($result)) {
		
		$fields = mysql_list_fields($database, $row[0], $cnConnection);
		$columns = mysql_num_fields($fields);
	
		for ($i = 0; $i < $columns; $i++) {
		 $colonnaTabella[] = mysql_field_name($fields, $i);
		 $tipoColonna[]  = mysql_field_type($fields, $i);
	
		}
		
		// CONNECT TO DATABASE
		mysql_select_db($database, $cnConnection);
		$query_rsRecordset = "SELECT * FROM ".$row[0];
		$rsRecordset = mysql_query($query_rsRecordset, $cnConnection) or die(mysql_error());
		$row_rsRecordset = mysql_fetch_assoc($rsRecordset);
		$totalRows_rsRecordset = mysql_num_rows($rsRecordset);
		
		// START STORING DATA IN VARIABLE TO PLACE IN XML FILE
			if($totalRows_rsRecordset > 0) {
			
			    $strXML = "<?xml version=\"1.0\"?>\n";
				
			    // STORE NAME OF TABLE
			    $strXML = $strXML . "		<".$row[0].">\n";
				
			    // STORE FIELD AND FIELD DATA IN ONE HIARCHY, REPEAT FOR MULTIPLE FIELDS
			    do {
	
					$strXML = $strXML . "				<record>\n";
					foreach ($colonnaTabella as $key => $value) {
    			
    					//Select the type of column and relative results
						$kindOfColumn = $tipoColonna[$key];
							switch ($kindOfColumn){
							case 'string':
							  $strXML = $strXML."						<".$value."><![CDATA[".$row_rsRecordset[$value]."]]></".$value.">\n";
							  break;
							case 'blob':
							  $strXML = $strXML."						<".$value."><![CDATA[".$row_rsRecordset[$value]."]]></".$value.">\n";
							  break;
							default:
							  $strXML = $strXML."						<".$value.">".$row_rsRecordset[$value]."</".$value.">\n";
	    				}
					}
					$strXML = $strXML . "				</record>\n";
			    } while ($row_rsRecordset = mysql_fetch_assoc($rsRecordset)); 
				
				$strXML = $strXML . "		</".$row[0].">";
			    
				// OPEN FILE, WRITE TO FILE, CLOSE FILE, CLOSE RECORDSET
			    $XMLFile = fopen($_SERVER['DOCUMENT_ROOT'].$directory.$row[0]."_xml.xml", "w") or die("can't open file");
			    
				fwrite($XMLFile, $strXML);
			    fclose($XMLFile);
			}
			
		mysql_free_result($rsRecordset);
		
		//Empty the arrays
		$colonnaTabella = array(); 
		$tipoColonna = array(); 
		
	}

	mysql_free_result($result);
	
	echo 'Db Processed...';


Rating:
51.0
9 votes
1 2 3 4 5

Commenti

XML to MYSQL by holyguard

11/06/2009 
Certo che c'è; , ce ne sono ben due, li puoi trovare QUI e QUI

Manca la connessione alla base di dati, che è facilmente aggiungibile alla fine del bucle per estrarre i dati XML.

Un saluto


MySql to XML converter by DGT

03/06/2009 
Ciao, ottimo script che mi sta facendo scoprire un nuovo mondo. Ce n'è per caso uno che facci il contrario? Cioè convertire da XML a MySql?

Ciao e grazie


Inserisci un commento

Titolo
Messaggio
Nome Utente
e-mail (se vuoi ricevere le risposte a questo post anche via mail)

Videos


Contenuti

Search Engine Optimization - 130 parametri usati da google per valutare il tuo sito

26/08/2010 

Con l'avvento di caffeine molti webmaster si sono trovati a dover rivedere tutte le strategie SEO per i…

in:Scripts e tutorials (0 commenti)

test

19/08/2010 

test

in:PHP Scripts (0 commenti)

Artweaver

28/05/2010 

Artweaver è un programma di disegno dotato di un'infinità di pennelli con cui sperimentare nuove tecniche grafiche.…

in:Software Open Source (0 commenti)

Configurare Domini e Sottodomini con BIND + Postfix

23/03/2010 

Con Bind, la configurazione di domini e sottodomini è abbastanza semplice....l'elemento più importante è il cosidetto zone-file, che…

in:Apache (0 commenti)

hkit - classi in php per il parsing dei microformats

05/03/2010 

hkit é un tool, o meglio, una classe in PHP5 per la estrazione dei piú comuni microformati.  La pagina…

in:PHP Scripts (0 commenti)

Certificati Java

01/03/2010 

Dopo circa un anno ho ritirato i certificati che attestano i miei corsi di Java...purtroppo manca il terzo…

in:Blog (3 commenti)

SVNWeaver, integrare subeversion in Dreamweaver

19/02/2010 

SVNWeaver é una estensione per Dreamweaver che permette di integrare il TortoiseSVN nei menú del Dreamweaver. Devo dire…

in:Applicazioni di sviluppo web (0 commenti)

Inserisci Google BUZZ nel tuo sito con Google Buzz Widget Jquery Plugin

15/02/2010 

Questo è un widget di jQuery per il nuovo social network di Google-Buzz-che è possibile incorporare ovunque per…

in:Scripts e tutorials (0 commenti)

Leggi tutto »


Tag Clouds


Search Engine Optimization parametri usati google valutare sitotestArtweaverConfigurare Domini Sottodomini BIND Postfixhkit classi parsing microformatsCertificati JavaSVNWeaver integrare subeversion DreamweaverInserisci Google BUZZ sito Google Buzz Widget Jquery Plugin


Add to Technorati Favorites