【发布时间】:2017-05-13 05:28:15
【问题描述】:
我有一个用作数据库的 XML 文档。它包含许多<product> 元素,每个元素都有<price>、<name>、<quantity> 等子元素...示例:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<product code="AS542EG2542">
<title>Best product in the market</title>
<category>something 1</category>
<price>10.99</price>
</product>
<product code="AS542EG2542">
<title>Second best product in the market</title>
<category>something 2</category>
<price>58.99</price>
</product>
<product code="AS542EG2542">
<title>Third best product in the market</title>
<category>something 2</category>
<price>24.99</price>
</product>
</catalog>
然后,在一个网站中,我使用 AJAX 来调用它,然后检索信息并将其插入到 DOM 中:
// Get the XML document
var request = $.ajax({
url: "http://www.example.com/resources/database.xml",
dataType: 'xml',
cache: 'false',
method: 'GET',
async: 'true'
});
// After AJAX has been completed, run the following function
request.done(function() {
testingFunction(request);
});
// The function that extracts data from XML and inserts it into the DOM
function testingFunction(xml) {
var xmlDoc = xml.responseXML,
xml = $( xmlDoc ),
// For each <product> elementt there is in the XML:
productsList = xml.find("product").each(function () {
// get child <title> of each <product>
var titleValue = $( this ).find("title").text();
// get child <price> of each <product>
var priceValue = $( this ).find("price").text();
// create a <li><div> titleValue </div><div> priceValue </div></li>
$("ul").append('<li><div class="iss-title">' + titleValue + '</div><div class="iss-price">' + priceValue + '</div</li>');
});
}
(我对编程比较陌生)
这是一种好的做法吗?我读过 XML 用于有组织的数据,而不是用于将它们存储为数据库。我还阅读了由例如提供的 SQL 数据库。 MySQL、PostgreSQL 等,我可以使用 PHP 从他们的数据库表中获取数据并将其注入 DOM。
在我的情况下,使用 MySQL 之类的 SQL 数据库会更好吗?遵循以下标准:
- 是一种很好的做法。例如。正如我所说的,XML 是用于以可读方式为程序传输的数据。
- Google bot 可以从 SQL 数据库中读取/抓取动态生成的内容。我读过 XML 和 it looks like Google won't crawl AJAX。
【问题讨论】:
-
为什么在
.done()回调中将request传递给testingFunction? googlebot 会抓取所有可以抓取的内容。 -
我认为您正在寻找评论。所以 codereview.stackexchange.com 是提出这类问题的合适场所。
-
@DanCostinel 嗯,基本上是的。我什至不知道它的存在——有很多 stackexchange 页面!
-
看看这个页面的底部。
标签: php jquery ajax xml database