PHP如何取XML中資料

來源:互聯網
上載者:User
'http://box.zhangmen.baidu.com/x?op=12&count=1&title=大約在冬季$$齊秦$$$$'
這個介面提供了xml格式的檔案,我用$xml = file_get_contents ( 'http://box.zhangmen.baidu.com/x?op=12&count=1&title=大約在冬季$$齊秦$$$$');取出來xml後如何得到裡面的值,求PHP大神寫出部分代碼給我參考,感激不盡。

  $xml = file_get_contents('http://box.zhangmen.baidu.com/x?op=12&count=1&title=大約在冬季$$齊秦$$$$');        $result = simplexml_load_string($xml);        var_dump($result); 


列印結果
object(SimpleXMLElement)[1]  public 'count' => string '1' (length=1)  public 'url' =>     object(SimpleXMLElement)[2]      public 'encode' =>         object(SimpleXMLElement)[5]      public 'decode' =>         object(SimpleXMLElement)[6]      public 'type' => string '8' (length=1)      public 'lrcid' => string '14706' (length=5)      public 'flag' => string '1' (length=1)  public 'durl' =>     object(SimpleXMLElement)[3]      public 'encode' =>         object(SimpleXMLElement)[7]      public 'decode' =>         object(SimpleXMLElement)[8]      public 'type' => string '8' (length=1)      public 'lrcid' => string '14706' (length=5)      public 'flag' => string '1' (length=1)  public 'p2p' =>     object(SimpleXMLElement)[4]      public 'hash' => string 'd1cae9f7634c5aa3d54d7b1b1ad4c5b79b6e97ff' (length=40)      public 'url' =>         object(SimpleXMLElement)[9]      public 'type' => string 'mp3' (length=3)      public 'size' => string '5710896' (length=7)      public 'bitrate' => string '192' (length=3)


我想取
http://zhangmenshiting.baidu.com/data2/music/64380827/Z2ZmbGVuaW9fn6NndK6ap5WXcGhomGieaZSZaGpvmGxlY2tnZ5ebaWNpamaaaZlrk2lqmZaXa3CTZJeZmW1ncGVll1qin5t1YWBkbGVpb2tiZGxqZ2drajE$64380827.mp3?xcode=43b1f8bf459a441832ad12772e3b3b77eaa48b2ded70843d&mid=0.18038312962142
encode裡面的資料。。請問該怎麼辦,那個object(SimpleXMLElement)[5]的值怎麼取出來。。


回複討論(解決方案)

$xml = file_get_contents('http://box.zhangmen.baidu.com/x?op=12&count=1&title=大約在冬季$$齊秦$$$$');          $result = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);var_dump($result->url->decode);

$xml = file_get_contents('http://box.zhangmen.baidu.com/x?op=12&count=1&title=大約在冬季$$齊秦$$$$');$result = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);$ret = (array)$result->url;echo $ret['encode']."
";echo $ret['decode'];


http://zhangmenshiting.baidu.com/data2/music/64380827/Z2ZmbGVuaW9fn6NndK6ap5WXcJZtb5lsamRjaW2ZnJmTaWxraWmYcWdoa2drZ26ZaZhlmZaXa3CTZJeZmW1ncGVll1qin5t1YWBmbWlvcHFlZmxqaWttbDE$
64380827.mp3?xcode=b89b492058ceab79743a96683617a8f2eaa48b2ded70843d&mid=0.39499944964564

xml = file_get_contents('http://box.zhangmen.baidu.com/x?op=12&count=1&title=大約在冬季$$齊秦$$$$');$result = simplexml_load_string($xml);echo strval($result->url->encode);echo strval($result->url->decode);
http://zhangmenshiting.baidu.com/data2/music/64380827/Z2ZmbGVuaW9fn6NndK6ap5WXcGVpZpuaZJdrbGtqmm1mY2lolppnmpSXZWZubZyclmlmmZaXa3CTZJeZmW1ncGVll1qin5t1YWBmbGxsamxlaGVrbG9taTE$64380827.mp3?xcode=140db3e8864c55164ad0bce2297ede73eaa48b2ded70843d&mid=0.38763446277961

百度知道上已經有人給解答了,用DOM實現的,感覺沒有你們寫的簡單。因為我不會PHP文法,所以這麼簡單的問題不會。。大家都對,結貼給分。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.