1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
// Ma propre fonction de traitement des balises ouvrantes
function fonctionBaliseOuvrante($parseur, $nomBalise, $tableauAttributs)
{
// mémoriser le nom de la balise afin d'en tenir compte dans la fonction "fonctionTexte"
global $derniereBaliseRencontree;
global $dernierTableauAttributRencontre;
global $s,$lgdon,$i,$j,$k,$z,$lg,$ln,$adr,$rep,$etiq,$don,$guide,$repEtiq,$repLg,$repDon,$notice,$tmp,$adrch,$champ,$x,$zone,$ok;
$derniereBaliseRencontree = $nomBalise;
$dernierTableauAttributRencontre = $tableauAttributs;
switch ($nomBalise)
{
case "RECORD":
$x=0;
break;
case "DATAFIELD":
$repEtiq[$x]=$dernierTableauAttributRencontre['TAG'];
if(!empty($dernierTableauAttributRencontre['IND1']))
$zone=$dernierTableauAttributRencontre['IND1'];
else
$zone=" ";
if(!empty($dernierTableauAttributRencontre['IND2']))
$zone.=$dernierTableauAttributRencontre['IND2'];
else
$zone.=" ";
break;
}
}
// Ma propre fonction de traitement des balises fermantes
function fonctionBaliseFermante($parseur, $nomBalise)
{
global $derniereBaliseRencontree;
global $dernierTableauAttributRencontre;
global $s,$lgdon,$i,$j,$k,$z,$lg,$ln,$adr,$rep,$etiq,$don,$guide,$repEtiq,$repLg,$repDon,$notice,$tmp,$adrch,$champ,$x,$zone,$ok;
global $cible;
switch ($nomBalise)
{
case "DATAFIELD":
if (!empty($zone))
{
$champ=$zone . chr(30);
$repLg[$x]=strlen($champ);
$repDon.=convertir2($champ);
$x+=1;
}
break;
case "RECORD" :
if ($ok==1)
{
$adr=24 + (12 * $x) + 1;
$adrdon=sprintf("%05s",$adr);
$guide = "nam1 22".$adrdon." i 450 ";
$adr=0;
$lg=0;
$lgdon="";
$rep="";
for ($j=0; $j<$x; $j++)
{
$adr = $adr + $lg;
$adrdon = sprintf("%05s",$adr);
$lg = $repLg[$j];
$lgdon = sprintf("%04s",$lg);
$rep .= $repEtiq[$j].$lgdon.$adrdon;
}
$rep.= chr(30);
$repDon.=chr(29);
$lg = 24 + strlen($rep) + strlen($repDon);
$lgdon = sprintf("%05s",$lg);
$guide = $lgdon.$guide;
$notice=$guide.$rep.$repDon;
// Ouverture du fichier
if (!$fcible = fopen($cible,"a"))
{
echo("<p>Impossible de créer le fichier cible ! </p>");
break;
}
fwrite($fcible,$notice);
}
break;
}
// On oublie la dernière balise rencontrée
// et tout le reste
$derniereBaliseRencontree = "";
$dernierTableauAttributRencontre = "";
}
// Ma propre fonction de traitement du texte
// qui est appelée par le "parseur"
function fonctionTexte($parseur, $texte)
{
global $derniereBaliseRencontree;
global $dernierTableauAttributRencontre;
global $s,$lgdon,$i,$j,$k,$z,$lg,$ln,$adr,$rep,$etiq,$don,$guide,$repEtiq,$repLg,$repDon,$notice,$tmp,$adrch,$champ,$x,$zone,$ok;
switch ($derniereBaliseRencontree)
{
case "CONTROLFIELD":
if($dernierTableauAttributRencontre['TAG']=='001')
{
$ok=1;
$repEtiq[$x]='001';
$champ=trim($texte).chr(30);
$repLg[$x]=strlen($champ);
$repDon=$champ;
$x+=1;
}
break;
case "SUBFIELD":
$tmp=trim($texte);
if(!empty($tmp))
{
$zone.=dollar.$dernierTableauAttributRencontre['CODE'];
$zone.=trim($texte);
}
break;
}
}
// Création du parseur XML
$parseurXML = xml_parser_create();
// Je précise le nom des fonctions à appeler
// lorsque des balises ouvrantes ou fermantes sont rencontrées
xml_set_element_handler($parseurXML, "fonctionBaliseOuvrante", "fonctionBaliseFermante");
// Je précise le nom de la fonction à appeler
// lorsque du texte est rencontré
xml_set_character_data_handler($parseurXML, "fonctionTexte");
// Ouverture du fichier
$fp = fopen($source, "r");
if (!$fp)
{
echo("<p>Impossible d'ouvrir le fichier source! </p>");
break;
}
// Lecture ligne par ligne
while ( $ligneXML = fgets($fp, 1024))
{
xml_parse($parseurXML, $ligneXML, feof($fp)) or die("Erreur XML");
}
xml_parser_free($parseurXML);
fclose($fp); |
Partager