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

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

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。