dtdvlazd56637 2013-07-08 03:24
浏览 54
已采纳

如何解析名称中包含“ - ”的xml标签?

i can't parse the tags containing "-" in their name in php with simplexml.

this is the xml- http://synd.cricbuzz.com/score-gadget/gadget-scores-feed.xml

the url-text and url-link could not be parsed.following errors accoured- 1)Use of undefined constant text - assumed 'text' in C:\wamp\www\score1.php 2)Use of undefined constant link - assumed 'link' in C:\wamp\www\score1.php 3)2)Use of undefined constant link - assumed 'url' in C:\wamp\www\score1.php plz help me php is parsing the url-text as seprate variables-"url" and "text"

this is the code

<?php 
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Pragma: no-cache"); Header('Pragma: no-cache');

    $url = "http://synd.cricbuzz.com/score-gadget/gadget-scores-feed.xml";
    //$url = "C:\wamp\www\score.xml";
    $curl = curl_init();
    curl_setopt ($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec ($curl);
    curl_close ($curl);
    print $result;
    $fp = fopen('score.xml', 'w');
    if($fp)
        fwrite($fp,  $result);  
    else
    echo "Error !";

    $url = "score.xml";
    $xml = simplexml_load_file($url,null, LIBXML_NOCDATA);
     foreach ($xml->match as $xmlinfo):
        $header=$xmlinfo->header;
        $description=$xmlinfo->description;
        $urltext=$xmlinfo->url-text;
        $urllink=$xmlinfo->url-link;
        echo $header,$description,$urltext;
    endforeach;
    //var_dump($xml);
 ?>
  • 写回答

1条回答 默认 最新

  • duanqian1888 2013-07-08 03:52
    关注

    Accessing elements within an XML document that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.

    In your case:

    $xmlinfo->{'url-text'}
    

    Live DEMO.

    Source Example #3

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码