Heim >Backend-Entwicklung >PHP-Tutorial >PHP怎么取XML中数据

PHP怎么取XML中数据

WBOY
WBOYOriginal
2016-06-13 11:51:27844Durchsuche

PHP如何取XML中数据
'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=大约在冬季$$齐秦$$$$');<br />        $result = simplexml_load_string($xml);<br />        var_dump($result); 


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


我想取
<encode><br /><![CDATA[<br />http://zhangmenshiting.baidu.com/data2/music/64380827/Z2ZmbGVuaW9fn6NndK6ap5WXcGhomGieaZSZaGpvmGxlY2tnZ5ebaWNpamaaaZlrk2lqmZaXa3CTZJeZmW1ncGVll1qin5t1YWBkbGVpb2tiZGxqZ2drajE$<br />]]><br /></encode><br /><decode><br /><![CDATA[<br />64380827.mp3?xcode=43b1f8bf459a441832ad12772e3b3b77eaa48b2ded70843d&mid=0.18038312962142<br />]]><br /></decode>
  encode里面的数据。。请问该怎么办,那个object(SimpleXMLElement)[5]的值怎么取出来。。
------解决方案--------------------
$xml = file_get_contents('http://box.zhangmen.baidu.com/x?op=12&count=1&title=大约在冬季$$齐秦$$$$');<br />	  <br />        $result = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);<br />		var_dump($result->url->decode);

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


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=大约在冬季$$齐秦$$$$');<br />$result = simplexml_load_string($xml);<br />echo strval($result->url->encode);<br />echo strval($result->url->decode);<br />
http://zhangmenshiting.baidu.com/data2/music/64380827/Z2ZmbGVuaW9fn6NndK6ap5WXcGVpZpuaZJdrbGtqmm1mY2lolppnmpSXZWZubZyclmlmmZaXa3CTZJeZmW1ncGVll1qin5t1YWBmbGxsamxlaGVrbG9taTE$<br />64380827.mp3?xcode=140db3e8864c55164ad0bce2297ede73eaa48b2ded70843d&mid=0.38763446277961<br />

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn