Ok, grazie, se ho capito bene ho trovato questo in rete...dovrebbe andare bene con accorgimenti ovviamente giusto?
creaxml.class.php
Codice PHP:
<?php
/*
Crea un file xml
*/
interface intCreaXML {} // fine interfaccia intCreaXML
class creaxml {
protected $dom = null;
function __construct() {} // __construct()
// Crea l'intestazione del file XML e impone la regola di indentare il codice per poterlo leggere meglio.
// Questa in una versione più avanzata potrebbe essere opzionale, dopotutto l'indentazione prende spazio ;-)
protected function inizializzaXML() {
$this->dom = new DOMDocument( '1.0', 'utf-8' );
$this->dom->preserveWhiteSpace = false;
$this->dom->formatOutput = true;
} // inizializzaXML()
// Crea un attributo con testo all'interno.
// @parent nodo del dom
// @nodo stringa (nodo dove va inserito l'attributo)
// @attributo stringa (nome dell'attributo)
// @testo stringa (valore dell'attributo)
protected function createAttributeText( $parent, $nodo, $attributo, $testo ) {
try {
$nodo = $parent->getElementsByTagName( $nodo )->item(0);
$attributo = $nodo->setAttributeNode( new DOMAttr( $attributo, $testo ));
} catch (DOMException $err) {
echo 'Errore! '. $err->getMessage();
exit();
}
} // createAttributeText()
// Crea un elemento XML e se previsto vi inserisce del testo
// @nodo nodo del dom
// @elemento stringa (nome dell'elemento da creare)
// @testo stringa (valore da inserire, può essere nullo per creare dei nodi vuoti)
protected function createElementText( $nodo, $elemento, $testo = null ) {
try {
$elemento = $nodo->appendChild( new DOMElement( $elemento ));
// Se c'è del testo (non nullo) lo scrive, se no passa oltre.
if ( !is_null( $testo ) ) {
$testo = $elemento->appendChild( new DOMText( $testo ));
}
} catch (DOMException $err) {
echo 'Errore! '. $err->getMessage();
exit();
}
} // createElementText()
// Crea un elemento XML e vi inserisce del testo in un campo CDATA come sopra ma con un campo CDATA.
protected function createElementTextCDATA( $nodo, $elemento, $testo ) {
try {
$elemento = $nodo->appendChild( new DOMElement( $elemento ));
$testo = $elemento->ownerDocument->createCDATASection( $testo );
$elemento->appendchild( $testo );
} catch (DOMException $err) {
echo 'Errore! '. $err->getMessage();
exit();
}
} // createElementTextCDATA()
// Salva il file
// @percorso stringa (percorso dove va salvato il file
// @nomeFile stringa (il nome del file :-))
// la funzione ottiene il LOCK del file in fase di scrittura.
protected function salvaXML( $percorso, $nomeFile ) {
// Salva il file nel percorso indicato, mentre lo salva ne acquisice il lock esclusivo.
file_put_contents( $percorso.$nomeFile, $this->dom->saveXML(), LOCK_EX );
unset( $this->dom ); // Libera la risorsa.
} // salvaXML()
} // fine classe intestazione
?>
questo è il miofile.php
Codice PHP:
<?php
require_once 'creaxml.class.php';
class mioXML extends creaxml {
function __construct( $nomefile ) {
$this->inizializzaXML();
$root = $this->dom->createElement( 'root' );
// Root del file
$this->createElementText( $root, 'lingua', 'it' );
$this->createElementText( $root, 'direzione', 'ltr' );
$this->createElementText( $root, 'codifica', 'utf-8' );
$this->dom->appendChild( $root );
// Salva il file ed esce
$this->salvaXML( 'files/', $nomefile.'.xml' );
} // __construct()
} // fine classe creaxml
$obj = new mioXML( $nomefile );
?>