Separate information from XML

1

How would I do to separate the issues according to the discipline of an xml file in PHP? Example: Mathematical answer c, d and a ... Physical answer c, d and E

<disciplina nome="Matemática">
         <questao numero="1" alternativas="A,B,C,D,E">
            <resposta>C</resposta>
         </questao>
         <questao numero="2" alternativas="A,B,C,D,E">
            <resposta>D</resposta>
         </questao>
         <questao numero="3" alternativas="A,B,C,D,E">
            <resposta>A</resposta>
         </questao>
      </disciplina>

      <disciplina nome="Física">
         <questao numero="1" alternativas="A,B,C,D,E">
            <resposta>C</resposta>
         </questao>
         <questao numero="2" alternativas="A,B,C,D,E">
            <resposta>D</resposta>
         </questao>
         <questao numero="3" alternativas="A,B,C,D,E">
            <resposta>A</resposta>
         </questao>
      </disciplina>
    
asked by anonymous 20.04.2016 / 21:22

1 answer

2

Use this function to move from xml to an array in php

I'm going to do an update of the function, because I found a method of a class that is not available here. So now it's complete.

/*
This function goes to the directory $pathDir, pass trough each all files inside it,
seeks for any file that math to the $searchString and reads it.
Return the content of all matched xml files within a php Array.
Ex: 
We have inside the FTP the follow files genereted by a CronJob: 
fileCarAudiRent2015.xml, fileCarAudiRent2016.xml, fileCarAudiSell2015.xm,
fileCarBMWRent2015.xml, fileCarBMWSell2016.xml, fileBoatWMBRent2015.xml, fileBoatWMBSell2015.xml,
fileBoatAudiRent2015.xml, fileBoatAudiSell2016.xml, ... .

We want to grab the content of all files that matches "Boat" string.
So, we need to do like this:
$arrInfo = read_XML_from_Folder_return_arrayAll("c:/Path_to_the_Folder","Boat");

We want to grab the content of all files that matches "Sell2015" string.
So, we need to do like this:
$arrInfo = read_XML_from_Folder_return_arrayAll("c:/Path_to_the_Folder","Sell2015");

We should inprove this function to match criteria like:
Give me all that has Sell AND 2015
Give me all that has Sell OR 2015
...
Give me all that has BMW AND 2015
*/


public function read_XML_from_Folder_return_arrayAll($pathDir,$searchString) {

$xml_array="";
$dir = new DirectoryIterator($pathDir); //http://php.net/manual/en/class.directoryiterator.php
foreach ($dir as $fileinfo) {
  if ($fileinfo->isFile()) {
      $fileName = $fileinfo->getFilename();
      //Macth rule
      $pos = strpos($fileName, $searchString);
      if ( $pos!== false) {//If match the sought string
        $filePath = $pathDir.$fileName;
        $xml = simplexml_load_file($filePath);
            $xml_array [] = unserialize(serialize(json_decode(json_encode((array) $xml), 1)));  

           /* If needed 
           $invoice_date = $xml_array["Documents"]["InvoiceDate"];
           $date = DateTime::createFromFormat('dmY', $invoice_date);//Formated date inside xml
           $year = $date->format('Y');//Desired output year
           $month = $date->format('M');//Desired output month 
           */

      }//End of if ( $pos!== false)                        
  } 
}
//Testing
echo "<pre>";
  print_r($xml_array);
echo "</pre>";


return $xml_array;

 }

Then it's easy. Make a loop in your php array and filter inside the loop. Font

    
20.04.2016 / 21:54