在codeigniter中读取并显示xml文件详细信息

时间:2017-01-04 06:37:34

标签: php xml codeigniter

我有一个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>

2 个答案:

答案 0 :(得分:0)

这是我用来简化您的SimpleXML库。将其解压缩到您的/ application / libraries文件夹

//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

只需循环访问相应的数组即可访问所需的数据。

答案 1 :(得分:0)

为了在视图文件上显示大小和名称。首先,您必须在控制器上获取名称和大小。

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

    }

 }

}

在此之后,您只需打印view

即可在$data中获取值
<?php print_r($data);?>