使用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

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

}