0
I need to format a xml in php to record on MySQL, but I’m having a hard time understanding it and even doing it.
Today I have this xml:
<?xml version='1.0' encoding='UTF-8'?>      
    <api>
       <response version='1.1.3' success='1'
                 message='Previs&#xE3;o de chuva - mm'
                 type='chuva' time='1491420459' total='1'>
          <legends />
       </response>
       <data>
          <item dateBegin='2017-04-05 00:00:00' dateEnd='2017-04-19 23:59:59'>
             <locale id='6634' name='NomeCidade'
                               latitude='-23.7060'
                               longitude='-51.6390'>
                <type id='7' name='CIDADE' />
             </locale>
             <weather>
                <item probability='80' precipitation='10' date='2017-04-05 00:00:00' />
                <item probability=''   precipitation='20' date='2017-04-06 00:00:00' />
                <item probability='80' precipitation='6'  date='2017-04-07 00:00:00' />
                <item probability='80' precipitation='2'  date='2017-04-08 00:00:00' />
                <item probability='80' precipitation='4'  date='2017-04-09 00:00:00' />
                <item probability='80' precipitation='5'  date='2017-04-10 00:00:00' />
                <item probability='0'  precipitation='0'  date='2017-04-11 00:00:00' />
                <item probability='0'  precipitation='0'  date='2017-04-12 00:00:00' />
                <item probability='0'  precipitation='0'  date='2017-04-13 00:00:00' />
                <item probability='0'  precipitation='0'  date='2017-04-14 00:00:00' />
                <item probability='0'  precipitation='0'  date='2017-04-15 00:00:00' />
                <item probability='0'  precipitation='0'  date='2017-04-16 00:00:00' />
                <item probability='0'  precipitation='0'  date='2017-04-17 00:00:00' />
                <item probability='80' precipitation='0'  date='2017-04-18 00:00:00' />
                <item probability='0'  precipitation='0'  date='2017-04-19 00:00:00' />
             </weather>
          </item>
       </data>
    </api>
And what I’m trying to do is something like this:
if ($status = true) 
{
    //Leitura do ramo Cidade
    for($i=0; $i < count($xml->cidades->cidade); $i++) 
    {
        $id   = $xml->cidades->cidade[$i]['id'];
        //Leitura do ramo Data e seus atributos
        for($z=0; $z < count($xml->cidades->cidade[$i]->data); $z++) 
        {
            $data = $xml->cidades->cidade[$i]->data[$z]['diaprevisao']; 
            $data_aux  = substr($data,6,4).'-'.substr($data,3,2).'-'.substr($data,0,2);
            //atributos da data
            $frase   = utf8_decode($xml->cidades->cidade[$i]->data[$z]->frase);
            $min       = utf8_decode($xml->cidades->cidade[$i]->data[$z]->min);
            $max       = utf8_decode($xml->cidades->cidade[$i]->data[$z]->max);
            $prob     = utf8_decode($xml->cidades->cidade[$i]->data[$z]->prob);
            $prec     = utf8_decode($xml->cidades->cidade[$i]->data[$z]->prec);
            $icomanha  = utf8_decode($xml->cidades->cidade[$i]->data[$z]->icomanha);
            $icotarde  = utf8_decode($xml->cidades->cidade[$i]->data[$z]->icotarde);
            $iconoite  = utf8_decode($xml->cidades->cidade[$i]->data[$z]->iconoite);
            $icodia     = utf8_decode($xml->cidades->cidade[$i]->data[$z]->icondia);
            $uv         = utf8_decode($xml->cidades->cidade[$i]->data[$z]->uv);
            $ventodir  = utf8_decode($xml->cidades->cidade[$i]->data[$z]->ventodir);
            $ventomax  = utf8_decode($xml->cidades->cidade[$i]->data[$z]->ventomax);
            $ventoint  = utf8_decode($xml->cidades->cidade[$i]->data[$z]->ventoint);
            $umidade   = utf8_decode($xml->cidades->cidade[$i]->data[$z]->umidade);
            mysql_select_db($database_con, $conn);
            $sql = "SELECT Data, IdCidade FROM PrevisaoClima WHERE Data = '$data_aux' AND IdCidade = $id";
            $rcDados = mysql_query($sql, $conn) or die(mysql_error());
            $row_rcDados = mysql_fetch_assoc($rcDados);
            $totalRows_rcDados = mysql_num_rows($rcDados);
            if ($totalRows_rcDados > 0) {
                mysql_select_db($database_con, $conn);
                $sql = "UPDATE `clima`.`PrevisaoClima` SET `Frase`='$frase', `TempMin`='$min', `TempMax`='$max', `ProbChuva`='$prob', `PrecisaoChuva`='$prec', `IconeManha`='$icomanha', `IconeTarde`='$icotarde', `IconeNoite`='$iconoite', `IconeDia`='$icodia', `VentoDir`='$ventodir', `VentoMax`='$ventomax', `VentoMin`='$ventoint', `Umidade`='$umidade', `Uv`='$uv' WHERE `Data`='$data_aux' AND `IdCidade`=$id";
                $result = mysql_query($sql, $conn);
                if (!$result) {
                    $message  = 'ERRO: ' . mysql_error() . "\n";
                    $message .= 'Query: ' . $sql;
                    die($message);
                }
            } else {
                mysql_select_db($database_con, $conn);
                $sql = "INSERT INTO `clima`.`PrevisaoClima` (`Data`, `IdCidade`, `Frase`, `TempMin`, `TempMax`, `ProbChuva`, `PrecisaoChuva`, `IconeManha`, `IconeTarde`, `IconeNoite`, `IconeDia`, `VentoDir`, `VentoMax`, `VentoMin`, `Umidade`, `Uv`) VALUES ('$data_aux', '$id', '$frase', '$min', '$max', '$prob', '$prec', '$icomanha', '$icotarde', '$iconoite', '$icodia', '$ventodir', '$ventomax', '$ventoint', '$umidade', '$uv')";
                $result = mysql_query($sql, $conn);
                if (!$result) {
                    $message  = 'ERRO: ' . mysql_error() . "\n";
                    $message .= 'Query: ' . $sql;
                    die($message);
                }
            }
        }
    }
}
echo "Importação terminada.";
Putting nodes in variables php.
you’re using
simple xml?– RFL
Hello @Rafaelacioly, I’m using file_get_contents
– adventistapr
I understood that its greatest difficulty was to access the positions, I made an answer in this sense, if you have any more questions, we are there ... Item 3 is easy to understand the process of accessing each item.
– novic