Search code examples
phpxmlsimplexmlcodeigniter-4

Codeigniter 4 and SimpleXMLElement


after leaving programming about 10 years ago, i am trying to use Codeigniter 4. My problem is getting an array from an XML file. So I was thinking of using PHP's SimpleXMLElement but I can't access this class because it is being searched in the wrong path. Here is an example of my code:

<?php namespace App\Controllers;

class Helloworld extends BaseController
{
    public function index()
    {

        $xmlstr = '<libri>
        <libro> 
            <autore>J.K.Rowlings</autore> 
            <titolo>Harry Potter e la pietra filosofale</titolo> 
            <editore>Salani</editore> 
        </libro>
        <libro> 
            <autore>J.R.Tolkien</autore> 
            <titolo>Il signore degli Anelli</titolo> 
            <editore>Bonpiani</editore> 
        </libro> 
        </libri>';

        $xml = new SimpleXMLElement($xmlstr);

    }
}

And this is the error i get:

Class 'App\Controllers\SimpleXMLElement' not found

Could someone help me?


Solution

  • Just use this

    new \SimpleXMLElement($xmlstr);
    

    Add a backslash befor SimpleXMLElement