Homepage Site Map


   
PROGRAMMAZIONE
   FARE WEB
   HTML
   CSS
   PHP
   MySQL
   XML - XSL
GRAFICA
   PAINT SHOP
   ANIMATION SHOP
   SWISH
   SWISHMAX
SCRIPT
   JAVASCRIPT
   PHP
   APPLET JAVA
   CSS GENERATOR
   WEB TOOLS
DOWNLOAD
   FONTS
   ICONE E AVATAR
   SMILES
   GIF ANIMATE
   CURSORI
   CALENDARI
   KAOANI
   BASI BANNER
   SCREENMATES
   MEEGOS
   GIOCHI
   VIDEO E PPS
ARTICOLI
   ARTICOLI
   GUADAGNA SITO
LINKS
   LINK TO ME
   VOTAMI
   SCAMBIO LINK
   SCAMBIO BANNER


zanox Partner promuovono Partner


 
  

Home -> Manuali e Tutorials -> Guida XML -> PHP XML File  


Scarica il tutorial      |    Stampa il tutorial      |    Cerca nel sito      



CREARE UN FILE XML CON PHP



Vediamo come creare l'xml come semplice file di testo; supponiamo che il codice XML da realizzare sia il seguente:


<?xml version="1.0"?>
<!DOCTYPE libreria [
     <!ELEMENT libreria (categorie)?>
     <!ELEMENT catgorie ((gialli)?,(romanzi)?,(cultura)?)>
     <!ELEMENT gialli (libro)+>
     <!ELEMENT romanzi (libro)+>
     <!ELEMENT cultura (libro)+>
     <!ELEMENT libro (autore,casaeditrice,titolo,anno,genere,prezzo)>
          <!ELEMENT autore (#PCDATA)>
          <!ELEMENT casaeditrice (#PCDATA)>
          <!ELEMENT titolo (#PCDATA)>
          <!ELEMENT anno (#PCDATA)>
          <!ELEMENT genere (#PCDATA)>
          <!ELEMENT prezzo (#PCDATA)>
]>

<libreria>
     <categorie>
          <gialli>
               <libro>
                    <autore>Agata Christy</autore>
                    <casaeditrice>nn lo so</casaeditrice>
                    <titolo>aboooooooo</titolo>
                    <anno>1980</anno>
                    <genere>Thriller</genere>
                    <prezzo>30</prezzo>
               </libro>
          </gialli>
          <romanzi>
               <libro>
                     <autore>Frewre</autore>
                     <casaeditrice>Mi nonna</casaeditrice>
                     <titolo>Amore folle</titolo>
                     <anno>1990</anno>
                     <genere>amore</genere>
                     <prezzo>20</prezzo>
               </libro>
          </romanzi>
          <cultura>
               <libro>
                    <autore>TRewvvc</autore>
                    <casaeditrice>De Agostini</casaeditrice>
                    <titolo>L'Egitto</titolo>
                    <anno>2003</anno>
                    <genere>geografia</genere>
                    <prezzo>50</prezzo>
               </libro>
          </cultura>
     </categorie>
</libreria>


Il codice PHP necessario per generare tale file XML sarà il seguente:


<?
$stringa_xml_dtd = "<?xml version=\"1.0\"?>\n<!DOCTYPE libreria [\n <!ELEMENT libreria (categorie)?>\n\t <!ELEMENT catgorie ((gialli)?,(romanzi)?,(cultura)?)>\n\t\t <!ELEMENT gialli (libro)+>\n\t\t <!ELEMENT romanzi (libro)+>\n\t\t <!ELEMENT cultura (libro)+>\n\t\t <!ELEMENT libro (autore,casaeditrice,titolo,anno,genere,prezzo)>\n\t\t\t <!ELEMENT autore (#PCDATA)>\n\t\t\t <!ELEMENT casaeditrice (#PCDATA)>\n\t\t\t <!ELEMENT titolo (#PCDATA)>\n\t\t\t <!ELEMENT anno (#PCDATA)>\n\t\t\t <!ELEMENT genere (#PCDATA)>\n\t\t\t <!ELEMENT prezzo (#PCDATA)>\n]>\n";

$stringa_xml_doc = " <libreria>\n\t <categorie>\n\t\t <gialli>\n\t\t\t <libro>\n\t\t\t\t <autore>Agata Christy </autore>\n\t\t\t\t <casaeditrice>nn lo so </casaeditrice>\n\t\t\t\t <titolo>aboooooooo </titolo>\n\t\t\t\t <anno>1980 </anno>\n\t\t\t\t <genere>Thriller </genere>\n\t\t\t\t <prezzo>30 </prezzo>\n\t\t\t </libro>\n\t\t </gialli>\n\t\t <romanzi>\n\t\t\t <libro>\n\t\t\t\t <autore>Frewre </autore>\n\t\t\t\t <casaeditrice>Mi nonna </casaeditrice>\n\t\t\t\t <titolo>Amore folle </titolo>\n\t\t\t\t <anno>1990 </anno>\n\t\t\t\t <genere>amore </genere>\n\t\t\t\t <prezzo>20 </prezzo>\n\t\t\t </libro>\n\t\t </romanzi>\n\t\t <cultura>\n\t\t\t <libro>\n\t\t\t\t <autore>TRewvvc </autore>\n\t\t\t\t <casaeditrice>De Agostini </casaeditrice>\n\t\t\t\t <titolo>L'Egitto </titolo>\n\t\t\t\t <anno>2003 </anno>\n\t\t\t\t <genere>geografia </genere>\n\t\t\t\t <prezzo>50 </prezzo>\n\t\t\t </libro>\n\t\t </cultura>\n\t </categorie>\n </libreria>";

$stringa_xml = $stringa_xml_dtd.$stringa_xml_doc;

$file_name = "prova_xml.xml";
$file = fopen ($file_name,"w");
$num = fwrite ($file, $stringa_xml);

fclose($file);

echo("File XML creato con successo!!");
?>


dove \n serve per andare a capo, mentre \t per la tabulazione. Esso, come già detto, viene creato come un semplice file di testo e quindi vengono usate le funzioni natie per operare su files.





Torna su      |    Indice Guida      |    Pagina << 20 >>    



 


Cynegi Network


Vai al W3C CSS Validator toolkit.
Realizzato con PHP. Vai al sito ufficiale del PHP.
Testato anche con Mozilla Firefox. Vai al sito ufficiale.

Home     Contacts     Forum     FAQ     Newsletter     Mappa     Cerca     Link to me

  Vedi gli RSS del sito.      

-->  [Il tuo link qui]