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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog