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 />';
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀