Problems when importing an xml file into a MySQL database using PHP

0

Good day, I have the following problem, I hope you can help me. I want to import an XML file into a MySQL database using the following code. My XML file is the following:

<xml>
   <clubnet>01/12/2017</clubnet>
      <escuelas>
          <escuela>
               <escuela>01</escuela>
               <Domicilio>FAJARDO# 163</Domicilio>
               <ciudad>ZAMORA</ciudad>
               <estado>MICHOACAN</estado>
               <encargado>PROF. JUANPEREZ</encargado>
               <telefono>3511231222</telefono>
               <Nombre>TAEKWONDO</Nombre>
               <Registro>.</Registro>
               <Folio>0</Folio>
               <puesto>PROFESOR ESCUELA</puesto>
               <referencia>E.C</referencia>
               <status>0</status>
               <escuelapadre>.</escuelapadre>
       </escuela>
   </escuelas>
</xml>

AND THE PHP CODE IS THE FOLLOWING:

<?php 

$xml_file = 'escuelas_2.xml';

if (file_exists($xml_file)) {
  $xml = simplexml_load_file($xml_file);
  echo "ARCHIVO XML CARGADO EXITOSAMENTE <br><br>";
} 
else {
  exit('Error al intentar abrir el fichero '.$xml_file);
}

require 'conexion.php';

$count=0; 

foreach ($xml->escuelas as $escuela) {

      $qry = "INSERT INTO escuelas (escuela, Domicilio, ciudad, estado, encargado,telefono,Nombre , Registro, Folio,puesto, referencia,status,escuelapadre)
          VALUES ('$escuela->escuela','$escuela->Domicilio','$escuela->ciudad','$escuela->estado','$escuela->encargado','$escuela->telefono','$escuela->Nombre','$escuela->Registro','$escuela->Folio','$escuela->puesto','$escuela->referencia','$escuela->status','$escuela->escuelapadre')";

      $resultado=mysqli_query($link,$qry) or die(mysqli_error());
      if($resultado)
        echo "INSERCIÓN REALIZADA CON EXITO";
      else
        echo "INSERCIÓN NO REALIZADA";


      $count++;
}//foreach

echo "
"; echo "-------------------------------------------
"; echo "Total de escuelas importadas: $count properties
"; echo "-------------------------------------------
";

?>
    
asked by Condor_G 11.12.2017 в 18:13
source

2 answers

0

Try to do it like this:

  • You can use $xml->escuelas->children() to get the school arrangement.

  • And to access the properties of each $escuela , you can cast object to arrangement ( eg: $escuela = (array) $escuela; )

  • Example:

    <?php 
    
    $xml_file = 'escuelas_2.xml';
    
    if (file_exists($xml_file)) {
      $xml = simplexml_load_file($xml_file);
      echo "ARCHIVO XML CARGADO EXITOSAMENTE <br><br>";
    } 
    else {
      exit('Error al intentar abrir el fichero '.$xml_file);
    }
    
    require 'conexion.php';
    
    $count=0; 
    
    foreach ($xml->escuelas->children() as $escuela) {
    
          $escuela = (array) $escuela;
    
          $qry = "INSERT INTO escuelas (escuela, Domicilio, ciudad, estado,
          encargado,telefono,Nombre , Registro, Folio,puesto, referencia,status,escuelapadre)
          VALUES
          ('$escuela[escuela]','$escuela[Domicilio]','$escuela[ciudad]','$escuela[estado]',
          '$escuela[encargado]','$escuela[telefono]','$escuela[Nombre]','$escuela[Registro]',
          '$escuela[Folio]','$escuela[puesto]','$escuela[referencia]','$escuela[status]',
          '$escuela[escuelapadre]')";
    
          $resultado=mysqli_query($link,$qry) or die(mysqli_error());
          if($resultado)
            echo "INSERCIÓN REALIZADA CON EXITO";
          else
            echo "INSERCIÓN NO REALIZADA";
    
    
          $count++;
    }//foreach
    
    echo "
    "; echo "-------------------------------------------
    "; echo "Total de escuelas importadas: $count properties
    "; echo "-------------------------------------------
    ";
    
    ?>
    
        
    answered by 11.12.2017 / 19:21
    source
    0

    I recommend that you better work the xml with a library for example this link .

    Example

    use Prewk\XmlStringStreamer;
    use Prewk\XmlStringStreamer\Stream;
    use Prewk\XmlStringStreamer\Parser;
    
    // Prepare our stream to be read with a 1kb buffer
    $stream = new Stream\File("gigantic.xml", 1024);
    
    // Construct the default parser (StringWalker)
    $parser = new Parser\StringWalker();
    
    // Create the streamer
    $streamer = new XmlStringStreamer($parser, $stream);
    
    // Iterate through the '<customer>' nodes
    while ($node = $streamer->getNode()) {
        // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>"
        $simpleXmlNode = simplexml_load_string($node);
        echo (string)$simpleXmlNode->firstName;
    }
        
    answered by 11.12.2017 в 18:22