douluolan9101 2017-01-02 06:51
浏览 31
已采纳

如何使用PHP在这个xml中读取这个属性? [关闭]

http://idragonlk.com/channels.xml

This is the xml.

What wanna be is read this channel and programs in a loop.

<?php 
$xmlDoc = new DOMDocument();
$xmlDoc->load("channels.xml");

$xmlObject = $xmlDoc->getElementsByTagName('channel');
$itemCount = $xmlObject->length;

for ($i=0; $i < $itemCount; $i++){
    $title = $xmlObject->item($i)->getElementsByTagName('display-name')->item(0)->childNodes->item(0)->nodeValue;
    print "Finished Item $title n<br/>";
}
?>

I have tried this. But it prints the txt values. I'm unable to get attribute values.

Wanna get value of

channel id=' '

lang='' of display-name

icon src='' value

program start ='' value

program stop = '' value

program channel = '' value

in program rating value

Plz can anyone help me?

  • 写回答

1条回答 默认 最新

  • dth62818 2017-01-02 09:06
    关注

    Try this, Maybe Helpful to you.

    <?php
    
    /*CURL PHP library to get file contents*/
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL, "http://idragonlk.com/channels.xml");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
    /*End*/
    
    /*Takes a well-formed XML string and returns it as an object.*/
    $XMLOutPut = simplexml_load_string($output);
    
    
    //$channel_list = getChannel_list($XMLOutPut);
    //$programme_list = getProgramme_list($XMLOutPut);
    
    echo "<pre>";print_r(getChannel_list($XMLOutPut));
    echo "<pre>";print_r(getProgramme_list($XMLOutPut));
    exit;
    
    /*Function to fetch channel and programme*/
    
    //channel
    function getChannel_list($mainObj){$Result_List =  array();
        for($i=0;$i<count($mainObj->channel);$i++){
            $Result_List[] = objectToArray($mainObj->channel[$i]);
        }
        return $Result_List;
    }
    
    //programme
    function getProgramme_list($mainObj){$Result_List =  array();
        for($i=0;$i<count($mainObj->programme);$i++){
            $Result_List[] = objectToArray($mainObj->programme[$i]);
        }
        return $Result_List;
    }
    
    //Convert Object to array.
    function objectToArray($obj) {
        if (is_object($obj)) {$obj = get_object_vars($obj);}
    
        if (is_array($obj)) {return array_map(__FUNCTION__, $obj);}
            else {return $obj;}
    }
    ?>
    

    OUTPUT

    Array
    (
        [0] => Array
            (
                [@attributes] => Array
                    (
                        [id] => 4seven UK
                    )
    
                [display-name] => 4seven UK
                [icon] => Array
                    (
                        [@attributes] => Array
                            (
                                [src] => 4seven UK.png
                            )
    
                    )
    
                [url] => http://www.tvguide.co.uk
            )
    
        [1] => Array
            (
                [@attributes] => Array
                    (
                        [id] => 5STAR UK
                    )
    
                [display-name] => 5STAR UK
                [icon] => Array
                ......
                ......
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 需求高精度PT100设计电路和算法
  • ¥15 单片机配网,继电器开关,广播
  • ¥60 Qcustomplot绘制实时动态曲线
  • ¥20 运用matlab画x-y图
  • ¥15 用idea运行项目,运行tomcat报错:断言失败
  • ¥15 Sqlserver查询链接服务器数据问题
  • ¥15 Bibtex4Word 引用中文文献
  • ¥20 用opencv c/c++ 转换成灰度图,然后做一下直方图均衡,输出mp4文件
  • ¥20 matlab中的双层数值积分
  • ¥50 服务器打印水晶报表问题