我想用php讀取xml的三個值

來源:互聯網
上載者:User
$url="http://www.cailele.com/static/ssc/newlyopenlist.xml";$doc = new DOMDocument();$doc->load($url); //讀取xml檔案$lst = $doc->getElementsByTagName('row');for ($i=0; $i<2; $i++) {$iframe= $lst->item($i);$t1=$iframe->attributes->getNamedItem('expect')->value;$t2=$iframe->attributes->getNamedItem('opencode')->value;$t3=$iframe->attributes->getNamedItem('opentime')->value;$t1=substr($t1,-9);kjdata($t2,1,$t1,$t3); }  

我想讀取xml的expec,opencode,opentime三個值,不知道為什麼讀取不上,求解答


回複討論(解決方案)

$xml = <<xml;$doc = new DOMDocument();$doc->loadXML($xml); //讀取xml檔案$items = $doc->getElementsByTagName('row');for ( $i  =  0 ;  $i  <  $items -> length ;  $i ++) {$t1 = $items -> item ( $i )->getAttribute('expect');//expect的值    $t2 = $items -> item ( $i )->getAttribute('opencode');//opencode的值    $t3 = $items -> item ( $i )->getAttribute('opentime');//opentime的值    var_dump($t1.'|'.$t2.'|'.$t3);}

經測試,你的代碼沒有問題
問題在於 $doc->load($url); 取不到值
該 url 用 file_get_contents 也取不到值
只有用 curl 才可取到

取到後可用 $doc->loadXML($str) 載入
然後就可用你的代碼處理了

經測試,你的代碼沒有問題
問題在於 $doc->load($url); 取不到值
該 url 用 file_get_contents 也取不到值
只有用 curl 才可取到

取到後可用 $doc->loadXML($str) 載入
然後就可用你的代碼處理了


能不能幫我發一下改了以後的代碼,萬分感謝

$url = "http://www.cailele.com/static/ssc/newlyopenlist.xml";$doc = new DOMDocument();$doc->loadXML(curl_get($url));$lst = $doc->getElementsByTagName('row');for ($i=0; $i<2; $i++) {  $iframe= $lst->item($i);  $t1=$iframe->attributes->getNamedItem('expect')->value;  $t2=$iframe->attributes->getNamedItem('opencode')->value;  $t3=$iframe->attributes->getNamedItem('opentime')->value;  $t1=substr($t1,-9);  kjdata($t2,1,$t1,$t3);}function kjdata() {  echo join(', ', func_get_args()), PHP_EOL;}function curl_get($durl) {  $cookiejar = realpath('cookie.txt');  $t = parse_url($durl);  $ch = curl_init();  curl_setopt($ch, CURLOPT_URL,$durl);  curl_setopt($ch, CURLOPT_TIMEOUT,5);  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);  curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);  curl_setopt($ch, CURLOPT_REFERER, "http://$t[host]/");  curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejar);  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  $r = curl_exec($ch);  curl_close($ch);  return $r;}
5,9,6,4,2, 1, 140421023, 2014-04-21 01:55:30
7,6,4,2,4, 1, 140421022, 2014-04-21 01:50:30

不錯不錯不錯不錯

$url = "http://www.cailele.com/static/ssc/newlyopenlist.xml";$doc = new DOMDocument();$doc->loadXML(curl_get($url));$lst = $doc->getElementsByTagName('row');for ($i=0; $i<2; $i++) {  $iframe= $lst->item($i);  $t1=$iframe->attributes->getNamedItem('expect')->value;  $t2=$iframe->attributes->getNamedItem('opencode')->value;  $t3=$iframe->attributes->getNamedItem('opentime')->value;  $t1=substr($t1,-9);  kjdata($t2,1,$t1,$t3);}function kjdata() {  echo join(', ', func_get_args()), PHP_EOL;}function curl_get($durl) {  $cookiejar = realpath('cookie.txt');  $t = parse_url($durl);  $ch = curl_init();  curl_setopt($ch, CURLOPT_URL,$durl);  curl_setopt($ch, CURLOPT_TIMEOUT,5);  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);  curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);  curl_setopt($ch, CURLOPT_REFERER, "http://$t[host]/");  curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejar);  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  $r = curl_exec($ch);  curl_close($ch);  return $r;}
5,9,6,4,2, 1, 140421023, 2014-04-21 01:55:30
7,6,4,2,4, 1, 140421022, 2014-04-21 01:50:30




非常感謝,分都給你了,我還有別的問題,想發帖沒分了,可不可以給我點分

給了你 200 可用分

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.