- Fichier test.xml
- -----------------
- <?xml version="1.0" encoding="UTF-8"?>
-
- <exportcontact>
- <contact>
- <nom>Toto</nom>
- <prenom>Titi</prenom>
- <numero type="tel portable" >0633333330</numero>
- </contact>
-
- <contact>
- <nom>dupont</nom>
- <prenom>Pierre</prenom>
- <numero type="tel fixe" >04946986980</numero>
- </contact>
-
- </exportcontact>
-
-
- Fonction
- -------------
- <?php
- function insertXml ($arrElem, $arrChamps, $table, $fichierXml) {
- $xml_parseur = xml_parser_create();
- $fp = fopen($fichierXml, "r") or die("Fichier introuvable. L'analyse a ete suspendue");
- while ($fdata = fread($fp, filesize ($fichierXml))){
- xml_parse_into_struct ($xml_parseur, $fdata, $arrOutput) or die (sprintf("Erreur XML : %s à la ligne %d\n",
- xml_error_string(xml_get_error_code($xml_parseur)),
- xml_get_current_line_number($xml_parseur))
- );
- }
-
- foreach ($arrOutput as $elem) {
- if (in_array ($elem['tag'], $arrElem)) {
- $arrReq[$elem['tag']][] = $elem['value'];
- if (is_array ($elem['attributes'])){
- foreach ($elem['attributes'] as $clef => $attr) {
- if (in_array ($clef, $arrElem)) {
- $arrReq[$clef][] = $attr;
- }
- }
- }
- }
- }
- if (is_array ($arrReq)) {
- $cpt = count ($arrReq[$arrElem[0]]);
- $strChamps = '';
- foreach ($arrChamps as $champ) {
- $strChamps .= '"'.$champ.'",';
- }
- $strChamps = rtrim ($strChamps, ',');
- $i = 0;
- while ($i < $cpt) {
- $strTmp = '';
- foreach ($arrReq as $clef => $dump) {
- $strTmp .= '"'.$arrReq[$clef][$i].'",';
- }
- $strTmp = rtrim ($strTmp, ',');
- $requeteTmp[] = $strTmp;
- $i ++;
- }
- $i = 0;
- while ($i < $cpt) {
- $requete[] = 'INSERT INTO '.$table.' ('.$strChamps.') VALUES ('.$requeteTmp[$i].')';
- $i ++;
- }
- return $requete;
- }
- return false;
- }
- ?>
-
- Exemple d'appel
- ----------------
- <?php
- $arrElem = array ('NOM', 'PRENOM', 'NUMERO', 'TYPE');
- $arrSql = array ('nom', 'prenom', 'telephone', 'type');
- if (($arrRequetes = insertXml ($arrElem, $arrSql, 'matable', 'test.xml'))!== false) {
- echo 'OK<pre>';
- print_r ($arrRequetes);
- echo '</pre>';
- }
- else
- echo '<br /><br />Erreur, false retourne';
- ?>
Fichier test.xml
-----------------
<?xml version="1.0" encoding="UTF-8"?>
<exportcontact>
<contact>
<nom>Toto</nom>
<prenom>Titi</prenom>
<numero type="tel portable" >0633333330</numero>
</contact>
<contact>
<nom>dupont</nom>
<prenom>Pierre</prenom>
<numero type="tel fixe" >04946986980</numero>
</contact>
</exportcontact>
Fonction
-------------
<?php
function insertXml ($arrElem, $arrChamps, $table, $fichierXml) {
$xml_parseur = xml_parser_create();
$fp = fopen($fichierXml, "r") or die("Fichier introuvable. L'analyse a ete suspendue");
while ($fdata = fread($fp, filesize ($fichierXml))){
xml_parse_into_struct ($xml_parseur, $fdata, $arrOutput) or die (sprintf("Erreur XML : %s à la ligne %d\n",
xml_error_string(xml_get_error_code($xml_parseur)),
xml_get_current_line_number($xml_parseur))
);
}
foreach ($arrOutput as $elem) {
if (in_array ($elem['tag'], $arrElem)) {
$arrReq[$elem['tag']][] = $elem['value'];
if (is_array ($elem['attributes'])){
foreach ($elem['attributes'] as $clef => $attr) {
if (in_array ($clef, $arrElem)) {
$arrReq[$clef][] = $attr;
}
}
}
}
}
if (is_array ($arrReq)) {
$cpt = count ($arrReq[$arrElem[0]]);
$strChamps = '';
foreach ($arrChamps as $champ) {
$strChamps .= '"'.$champ.'",';
}
$strChamps = rtrim ($strChamps, ',');
$i = 0;
while ($i < $cpt) {
$strTmp = '';
foreach ($arrReq as $clef => $dump) {
$strTmp .= '"'.$arrReq[$clef][$i].'",';
}
$strTmp = rtrim ($strTmp, ',');
$requeteTmp[] = $strTmp;
$i ++;
}
$i = 0;
while ($i < $cpt) {
$requete[] = 'INSERT INTO '.$table.' ('.$strChamps.') VALUES ('.$requeteTmp[$i].')';
$i ++;
}
return $requete;
}
return false;
}
?>
Exemple d'appel
----------------
<?php
$arrElem = array ('NOM', 'PRENOM', 'NUMERO', 'TYPE');
$arrSql = array ('nom', 'prenom', 'telephone', 'type');
if (($arrRequetes = insertXml ($arrElem, $arrSql, 'matable', 'test.xml'))!== false) {
echo 'OK<pre>';
print_r ($arrRequetes);
echo '</pre>';
}
else
echo '<br /><br />Erreur, false retourne';
?>