bourax webmaster
bourax webmaster

Reputation: 733

XML symfony data retrieval

I have a XML file in which I saved some product details: ID, name, price

How can I display these rows in a table? Should I use doctrine? Where should I put the XML file? In public and then install assets?

Here is my products.xml

<products>
<product>
    <id>2222</id>
    <manufacturer>AMINE</manufacturer>
    <name>*AMINE    123    185/65 R15 88 T</name>
    <additional>AUSLAUF</additional>
    <price>74.24</price>
    <availability>4</availability>
    <product_image>http://media2.tyre24.de/images/tyre/330-R-w300-h300-br1.jpg</product_image>
</product>
<product>
    <id>3333</id>
    <manufacturer>AMINE</manufacturer>
    <name>*AMINE    456    185/65 R15 88 T</name>
    <additional>AUSLAUF</additional>
    <price>74.24</price>
    <availability>4</availability>
    <product_image>http://media2.tyre24.de/images/tyre/330-R-w300-h300-br1.jpg</product_image>
</product>
<product>
    <id>3333</id>
    <manufacturer>AMINE</manufacturer>
    <name>*AMINE    789    185/65 R15 88 T</name>
    <additional>AUSLAUF</additional>
    <price>74.24</price>
    <availability>4</availability>
    <product_image>http://media2.tyre24.de/images/tyre/330-R-w300-h300-br1.jpg</product_image>
</product>

Upvotes: 0

Views: 159

Answers (1)

Gara
Gara

Reputation: 626

use 'simplexml_load_file($url);' function. This function will convert an XML file into a SimpleXMLElement object, then output keys and elements of the object.

After this 'json_decode(json_encode((array)$var_name),1)' this function will convert the that object to an json object. By using this josn object we can do what ever we want.

 public function functionNameAction(){
    // Write your code
    try {
        $simpleXMLElementObject = simplexml_load_file($file_url);
        $xml_josn_object = json_decode(json_encode((array)$simpleXMLElementObject),1);
    }catch (\Exception $e){
        // Handle the exception 
    }

    // Render your template and pass this $xml_josn_object into the template and do what ever you want in that template
}

For reference just Look this

Upvotes: 1

Related Questions