Validate xml file before updating database

4

I have a file upload system that when sending the xml file it sends the inserts and updates to the database, but if I send a file with the different structure I get errors. I have already found some solutions using DTD and #

How can I ensure that the uploaded file actually has the desired structure using php and mysql?

Here is an example of the xml file:

<?xml version="1.0"?>
<table>
    <row>
        <column>
            131290444
        </column>
        <column>
            Nome Completo
        </column>
        <column>
            10/09/1991
        </column>
        <column>
            39077161830
        </column>
        <column>
            [email protected]
        </column>
        <column>
            Aluno Regularmente Matriculado
        </column>
    </row>
    <row>
        <column>
            151290202
        </column>
        <column>
            Nome completo 2
        </column>
        <column>
            20/09/1987
        </column>
        <column>
            37999131814
        </column>
        <column>
            [email protected]
        </column>
        <column>
            Aluno Regularmente Matriculado
        </column>
    </row>
</table>
    
asked by anonymous 11.03.2015 / 03:43

2 answers

1

To work with XML in PHP I like to use the XMLReader and XMLWriter classes because they process node by node instead of loading everything in memory as do other XML manipulation classes with PHP.

Without using the DTD or XML Schema you can use the XMLReader to find the nodes and compare them to a pattern you like.

Example - check if each row has 6 columns and check the data for each column

<?php
$error = array();
$xml_file = 'arquivo.xml';
$reader = new XMLReader();
$reader->open($xml_file);
$r = 0;
while ($reader->read()){//enquanto tiver nós no xml
  if ($reader->nodeType == XMLREADER::ELEMENT && $reader->name === 'row'){ //acha um row
    $r++;
    $row = new XMLReader; //nova instância para ler o xml dentro do nó row
    $r = $reader->readOuterXML(); //pega todo o nó
    $row->xml($r); //e carrega o nó na nova instância xml
    $c = 0; 
     while ($row->read()){//enquanto tiver nós no row  
        if ($row->nodeType == XMLREADER::ELEMENT && $reader->name !== 'column'){//verifica se o nó é diferente de <column>
        $c++;
             $error[] = 'Nó ' . $c . ' do row ' . $r . ' diferente de column';
        }
        if ($row->nodeType == XMLREADER::ELEMENT && $reader->name !== 'column'){
        //encontramos uma coluna
        $c++;
            $conteudo = $row->readInnerXML(); //pega o conteúdo do nó
            //aqui você pode testar cada tipo de dado de acordo com o número do nó
        }
     }//acabaram os nós do row    
    if ($c !== 6) {
        $error[] = 'Row '.$r.'tem um total de '.$c.' colunas.';//se não tiver 6 colunas
  }
}//acabaram as rows
var_dump ($error); //dump dos erros 
?>

This is just an example to get you started, I have not tested it. But in this way you can process XMLs of any size without consuming too many resources of your server. Check out XMLReader manual for more function options.

    
12.03.2015 / 02:06
0

You can simply read your file and check the number of columns for each item in your XML:

$xmlFile = <<<EOF
<?xml version="1.0"?>
<table>
    <row>
        <column>
            131290444
        </column>
        <column>
            Nome Completo
        </column>
        <column>
            10/09/1991
        </column>
        <column>
            39077161830
        </column>
        <column>
            [email protected]
        </column>
        <column>
            Aluno Regularmente Matriculado
        </column>
    </row>
    <row>
        <column>
            151290202
        </column>
        <column>
            Nome completo 2
        </column>
        <column>
            20/09/1987
        </column>
        <column>
            37999131814
        </column>
        <column>
            [email protected]
        </column>
        <column>
            Aluno Regularmente Matriculado
        </column>
    </row>
</table>
EOF;

function verifyRolesInXMLFile($urlFile) {

    try {

           //lê o arquivo XML
             $xml = new SimpleXMLElement($urlFile);

           if (count($xml->row) != 2) {
              throw new Exception("Número de <rows> tem que ser 2, o total deu: ".count($xml->row));
           } 

           if (count($xml->row[0]->column) != 6) {
              throw new Exception("Número de <column> da primeira linha tem que ser 6, o total deu: ".count($xml->row[0]->column));
           } 

           if (count($xml->row[1]->column) != 6) {
              throw new Exception("Número de <column> da linha de baixo tem que ser 6, o total deu: ".count($xml->row[1]->column));
           } 
          echo $xml;
       } catch (\Exception $e) {
           echo $e->getMessage();
       }
}
//executa o método de verificação de colunas
verifyRolesInXMLFile($xmlFile);

See the solution working here

    
10.12.2015 / 17:34