0

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

5
  • Your xml is incomplete. Commented Sep 20, 2013 at 0:40
  • It is ok man, the end tag is missing here but not at my code. However is just fixed. Commented Sep 20, 2013 at 0:44
  • @DanielDake, are you sure it's opening application/libraries/xml.xml ? Commented Sep 20, 2013 at 1:00
  • its a small thing but having a closing ?> can mess up your php files in the framework - its recommended you don't use them. and the path to the xml file might not be complete. Commented Sep 20, 2013 at 1:57
  • @DrixsonOseña yes i am sure because is not giving me any error. Commented Sep 20, 2013 at 2:09

1 Answer 1

1

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

}
Sign up to request clarification or add additional context in comments.

2 Comments

ok i changed to this $parent_ids = $links->getElementsByTagName( "PARENT_ID" )->item(0)->nodeValue; what do i have to return instead ?
That was the trick and it make sense this way. Thank you @subroutines you save my night, now i can drink a beer in pace. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.