douyongwan5946 2013-09-20 00:37
浏览 48
已采纳

使用codeigniter循环XML数据

This now is getting weird. i have spend more than 3 hours to figure it out and til no result. I want to loop through a simple XML file and display it the results on the page.

Here is my XML code:

<?xml version="1.0"?>
<CATALOG>
<DATA>
<ENTITY_ID>0111</ENTITY_ID>
<PARENT_ID>0222</PARENT_ID>
<LEVEL>0333</LEVEL>
</DATA>
<DATA>
<ENTITY_ID>0111</ENTITY_ID>
<PARENT_ID>0222</PARENT_ID>
<LEVEL>0333</LEVEL>
</DATA>
</CATALOG>

Here is my Model:

<?php

class xmlmodel extends CI_Model{

public function catalog(){

    $doc = new DOMDocument();
    $path = 'application/libraries/xml.xml';
    $doc->load($path);//xml file loading here

    $data = $doc->getElementsByTagName('DATA');

    foreach($data as $links){

        $entity_ids = $links->getElementsByTagName('ENTITY_ID');

        $parent_ids = $links->getElementsByTagName( "PARENT_ID" );

        return $links;
       }}}

?>

Here is my controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

public function index()
{
    $this->load->model('xmlmodel');
    $data['category_catalog_entity'] = $this->xmlmodel->catalog();
    $this->load->view('welcome_message', $data);
}
}

And Here is my view:

<?php

foreach($category_catalog_entity as $result){

echo $result;

}
?>

Maybe you guys can give me another idea, for this way it does not make sense at all i don,t have any error just blank nothing is being displayed. Hope someone help me. Thank you

  • 写回答

1条回答 默认 最新

  • douketangyouzh5219 2013-09-20 01:58
    关注

    A couple of issues:

    1. Your catalog() function is returning an object. To get the value you need something like $entity_ids = $links->getElementsByTagName('ENTITY_ID')->item(0)->nodeValue;

    2. You're returning $links inside a loop, so your function will always return the first element.

    Your catalog function:

    public function catalog(){
    
        $doc = new DOMDocument();
        $path = 'application/libraries/xml.xml';
        $doc->load($path);//xml file loading here
    
        $data = $doc->getElementsByTagName('DATA');
    
        return $data;
    }
    

    Then in your view:

    foreach($category_catalog_entity as $result){
    
        echo $result->getElementsByTagName('ENTITY_ID')->item(0)->nodeValue . '<br />';
        echo $result->getElementsByTagName('PARENT_ID')->item(0)->nodeValue . '<br />';
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!