嗯,您希望事情如何发生并不是很清楚,因此很难就是否在服务器端或客户端消费 XML Feed 给出建议。
此外,您还没有解释如何“获取”xml 提要。它是需要身份验证的网络服务吗?它是什么类型的请求?或者它只是驻留在互联网某处的文件?还是在当地可以买到?
我将假设您使用的提要不需要复杂的请求或身份验证。
所以这是一个使用 jQuery 和 GET 请求的 AJAX 示例
XML 示例 (productsfeed.xml)
<products>
<product id="prod1" name="iMac" price="2000" vendor="Apple">
<stock>10</stock>
</product>
<product id="prod2" name="iPad" price="500" vendor="Apple">
<stock>50</stock>
</product>
<product id="prod3" name="Galaxy S3" price="500" vendor="Samsung">
<stock>100</stock>
</product>
</products>
带有 ajax 调用和产品下拉列表的示例 HTML。
<!DOCTYPE HTML>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$.ajax({
url: 'http://myhost.com/webservice', // Url to the webservice or feed
// Here goes all the "shenanigans" regarding the web service, like
// request type, headers to send, etc..
success: function(xml) {
$(xml).find('product').each(function(){
var id = $(this).attr('id');
var name = $(this).attr('name');
var price = $(this).attr('price');
var vendor = $(this).attr('vendor');
var stock = $(this).find('stock').text();
var $selectItem = $('<option></option>');
$selectItem.attr('value', id);
$selectItem.attr('data-id', id);
$selectItem.attr('data-price', price);
$selectItem.attr('data-vendor', vendor);
$selectItem.attr('data-stock', stock);
$selectItem.html(name);
$selectItem.appendTo('#plist');
});
}
});
});
</script>
</head>
<body>
<div><span>Product List</span></div>
<div id="plist-wrapper">
<select id="plist">
</select>
</div>
</body>
</html>
现在使用 PHP
如果它是一个网络服务,你可以使用 cURL
$url = 'http://myhost.com/webservice';
// HTTP Headers to send to the webservice
// specific to the webservice you're consuming
$headers = array();
$ch = curl_init();
// curl_setopt to set the options
// see http://www.php.net/manual/en/function.curl-setopt.php
curl_setopt($ch, CURLOPT_URL, $url);
//headers if needed
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Send the request and check the response
if (($result = curl_exec($ch)) === FALSE) {
echo 'Failed with' . curl_error($ch) . '<br />';
} else {
echo "Success!<br />\n";
}
curl_close($ch);
如果它不是网络服务(只是一个 xml 文件),您可以使用 file_get_contents
$result = file_get_contents('http://myhost.com/productsfeed.xml');
解析 xml 并显示下拉选择菜单
$xml = new DOMDocument();
$xml->loadXML($result);
$html = new DOMDocument();
$htmlSource =
'<html>
<head></head>
<body>
<div><span>Product List</span></div>
<div id="plist-wrapper">
<select id="plist"></select>
</div>
</body>
</html>';
$html->loadHTML($htmlSource);
$selectList = $html->getElementsByTagName('select')->item(0);
$optionItem = $html->createElement('option');
$prodNodeList = $xml->getElementsByTagName('product');
foreach ($prodNodeList as $prodNode) {
$id = $prodNode->getAttribute('id');
$name = $prodNode->getAttribute('name');
$price = $prodNode->getAttribute('price');
$vendor = $prodNode->getAttribute('vendor');
$option = clone $optionItem;
$option->setAttribute('value', $id);
$option->setAttribute('data-id', $id);
$option->setAttribute('data-price', $price);
$option->setAttribute('data-vendor', $vendor);
$option->nodeValue = $name;
$selectList->appendChild($option);
}
print $html->saveHTML();