I have a xml file as shown below.
How can I read and display the name and size in a view with help of a controller?
<?xml version="1.0" encoding="UTF-8"?>
<QCARConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="qcar_config.xsd">
<Tracking>
<ImageTarget name="news_opera_house" size="1200.000000 755.705811" />
<ImageTarget name="news_istambul" size="700.000000 415.091461" />
</Tracking>
</QCARConfig>
Here's a SimpleXML library I use to make this easier for you. Unzip it to your /application/libraries folder
//Load SimpleXML library
$this->load->library('simplexml');
$xmlData = $this->simplexml->xml_parse($xml); //where $xml is your xml to parse
var_dump($xmlData); //You should see an multi dimensional array for each node
Just loop through the appropriate arrays to access the data you need.
In order to display size and name on your view file.First you will have to get name and size on your controller.
public function index(){
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<QCARConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="qcar_config.xsd">
<Tracking>
<ImageTarget name="news_opera_house" size="1200.000000 755.705811" />
<ImageTarget name="news_istambul" size="700.000000 415.091461" />
</Tracking>
</QCARConfig>';
$xmlcont = new SimpleXMLElement($xml);
foreach ($xmlcont as $value) {
$val = $value->ImageTarget;
foreach ($val as $key) {
//echo $key['size']."<br/>";
//echo $key['name']."<br/>";
$data = array('size' => $key['size'],'name' => $key['name'] );
$this->load->view('main_view', $data); //passing value to view
}
}
}
After this you can simply get the values in view
by printing the $data
<?php print_r($data);?>