【发布时间】:2020-01-14 08:38:48
【问题描述】:
我正在制作一个商店概览页面,每页呈现 +- 20 个产品。我从压缩 (gzip) XML 文件 (*.xml.gz) 中获取数据。这是提要:http://www.endclothing.com/media/end_feeds/awin_affiliates_eu.xml.gz 每天一次,我使用 PHP 将文件下载到我的服务器并提取 XML 文件。
问题是,解压缩的 XML 文件是 +- 60MB 并且包含超过 50k 的产品。现在,当我尝试从 XML 文件中获取产品并显示它们时,进展非常缓慢。使用下面我使用的代码从本地 XML 显示产品信息大约需要 8 秒:
$.ajax({
type: "GET",
url: 'feeds/awin_affiliates_eu.xml',
cache: true,
dataType: "xml",
error: function (response) {
alert("An error occurred while processing XML file");
console.log('XML reading Failed: ', e);
},
success: function (response) {
var max = 20;
$(response).find("product").each(function (i) {
if (i < max) {
var _pid = $(this).find('pid').text();
var _mpn = $(this).find('mpn').text();
var _colour = $(this).find('colour').text();
var _name = $(this).find('name').text();
var _purl = $(this).find('purl').text();
var _instock = $(this).find('instock').text();
var _brand = $(this).find('brand').text();
var _suitable_for = $(this).find('suitable_for').text();
var _ptype = $(this).find('ptype').text();
var _category = $(this).find('category').text();
var _condition = $(this).find('condition').text();
var _desc = $(this).find('desc').text();
var _currency = $(this).find('currency').text();
var _custom1 = $(this).find('custom1').text();
var _price = $(this).find('price').text();
var _deltime = $(this).find('deltime').text();
var _delcost = $(this).find('delcost').text();
var _imgurl = $(this).find('imgurl').text();
var _alternate_image = $(this).find('alternate_image').text();
$("h2._name").eq(i).text(_name);
$(".price").eq(i).text(_price);
var background_url = "url(" + _imgurl + ")";
$(".panel").eq(i).css("background", background_url);
} else {
return false;
}
});
console.log('done reading file');
}
});
有什么方法可以更快地读取 XML 文件,从而更有效地呈现我的产品?
【问题讨论】:
-
It takes about 8 seconds to display product information下载 60MB 需要多少时间? (在 100mbit 的互联网连接上,没有其他任何事情发生,这至少需要 5 秒 - 所以,我猜 8 秒可能主要是下载时间 - 解决这个问题的唯一方法是更快的互联网连接)跨度> -
@epascarello -
return false将停止 jqueryeach循环 -
您可以设置一个 cron 作业,每天下载一次,解析数据并将您需要的数据存储在数据库中。然后应用程序的其余部分将只需要从数据库中查询数据。
-
Once a day I download the file to my server with PHP and extract the XML file- 你应该只在提取的 XML 中包含前 20 个产品 - PHP 肯定有能力以这种方式操作 XML 吗? -
@JaromandaX 根据我的 chrome 开发人员模式,60 MB 大约需要 800 毫秒。我现在也在当地工作。最大的停机时间是等待。
标签: javascript php jquery xml gzip