簡體   English   中英

使用php在網頁中顯示xml數據

[英]Display xml data in a webpage with php

我正在嘗試顯示從外部來源獲得的xml分析頁面的數據。 我通過像這樣的一些參數傳遞:-

http://www.somewebsite.com/phpfile.php?vendor_key=xxx&checkin=2012-11-02&checkout=2012-11-05&city_id=5&guests=3

當我傳遞此參數時,我得到了xml結果。 現在我想以一種設計器的方式在我的網頁上顯示該xml數據。 所以我該怎么辦。 我是xml的新手,所以如果有人可以告訴我這種技術叫什么,那么它也不知道該技術叫什么,這也可以幫助我。

看一下simplexml_load_string

您可以使用curlfile_get_contents函數發出HTTP請求。 然后,您可以使用DOMSimpleXML解析請求的URL的響應(XML)。

如果您已經擁有XMl,請嘗試

echo $xml->asXML();

一個完整的例子

<?php 
 $curl = curl_init();        
 curl_setopt ($curl, CURLOPT_URL, 'http://rss.news.yahoo.com/rss/topstories');   
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);   
 $result = curl_exec ($curl);   

 if ($result === false) {
      die('Error fetching data: ' . curl_error($curl));   
 }
   curl_close ($curl);    

//we can at this point echo the XML if you want
 //echo $result;

 //parse xml string into SimpleXML objects
  $xml = simplexml_load_string($result);

   if ($xml === false) {
      die('Error parsing XML');   
   }

   //now we can loop through the xml structure
   foreach ($xml->channel->item as $item) {
       print $item->title;   
   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM