XML : PHP - From DOMDocument to XMLReader

PHP developers here ?? I have a PHP function who parse an xml file (using DOMDocument, i'm proficien with this tool). I want to do the same with XMLReader, but i don't understand how XMLReader works...

I want to use XMLReader because it's a light tool.

Feel free to ask me others questions about my issue.

  function getDatas($filepath)  {      $doc = new \DOMDocument();      $xmlfile = file_get_contents($filepath);      $doc->loadXML($xmlfile);        $xmlcars = $doc->getElementsByTagName('car');      $mycars= [];        foreach ($xmlcars as $xmlcar) {          $car = new Car();          $car->setName(              $xmlcar->getElementsByTagName('id')->item(0)->nodeValue          );          $car->setBrand(              $xmlcar->getElementsByTagName('brand')->item(0)->nodeValue          );            array_push($mycars, $car);      }      return $mycars;     }    

PS : I'm not a senior PHP dev.

Ahah Thanks.

No comments:

Post a Comment