[英]Display xml data in a webpage with php
我正在嘗試顯示從外部來源獲得的xml分析頁面的數據。 我通過像這樣的一些參數傳遞:-
當我傳遞此參數時,我得到了xml結果。 現在我想以一種設計器的方式在我的網頁上顯示該xml數據。 所以我該怎么辦。 我是xml的新手,所以如果有人可以告訴我這種技術叫什么,那么它也不知道該技術叫什么,這也可以幫助我。
您可以使用curl或file_get_contents函數發出HTTP請求。 然后,您可以使用DOM
或SimpleXML
解析請求的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.