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

时间:2013-07-08 03:24:42

标签: php xml parsing

我无法使用simplexml解析php中名称中包含“ - ”的标签。

这是xml-      http://synd.cricbuzz.com/score-gadget/gadget-scores-feed.xml

无法解析url-text和url-link。以下错误: 1)使用未定义的常量文本 - 在C:\ wamp \ www \ score1.php中假定为“text” 2)使用未定义的常量链接 - 在C:\ wamp \ www \ score1.php中假定为“链接” 3)2)使用未定义的常量链接 - 在C:\ wamp \ www \ score1.php中假定为“url” 请帮助我 php正在将url-text解析为seprate variables-“url”和“text”

这是代码

<?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 个答案:

答案 0 :(得分:2)

  

访问包含非字符的XML文档中的元素   允许根据PHP的命名约定(例如连字符)   通过将元素名称封装在大括号和   撇号。

在你的情况下:

$xmlinfo->{'url-text'}

Live DEMO.

Source Example #3