在 xml 中搜索属性
Search for attribute in xml
我尝试了许多不同的方法,但没有运气。我所要做的就是识别当前 URL 的域,并根据域从 XML 文件中提取一个值。
任何帮助将不胜感激。
商人.xml
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<merchant id="1">
<domain>http://www.amazon.com</domain>
<affiliate>1</affiliate>
</merchant>
</catalog>
弹出窗口.js
function ReadXML() {
try {
var xmlPath = "merchants.xml";
$.ajax({
type: "GET",
url: xmlPath,
dataType: "xml",
success: parseXML
});
} catch (e) {
alert("Error while reading XML; Description – " + e.description);
}
}
function parseXML(xml) {
var $merchant = $(xml).find('domain').filter(function() {
return $(this).text() == "http://www.amazon.com";
}).closest('domain');
var affiliate = $('affiliate', $domain).text();
if(window.location.hostname.indexOf("http://www.amazon.com") > -1) {
document.getElementById("demo").innerHTML = affiliate;
}
}
readXML()
弹出窗口.html
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<style>
body {
font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
font-size: 75%;
}
#status {
/* avoid an excessively wide status text */
white-space: normal;
text-align: center;
width: 200px;
height: 225px;
overflow: hidden;
max-width: 400px;
word-wrap: normal;
}
</style>
<script type="text/javascript" src="popup.js"></script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<table id="demo"></table>
<div id="status"></div>
</body>
</html>
您可以使用 jQuery XML 转换为 JSON 插件将 XML 转换为 JSON。结果,您将获得一个包含所有属性的 Javascript 对象:
{"merchant":{"domain":"http://www.amazon.com","affiliate":"1","id":"1"}}
完整示例:
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
<script type="text/javascript" src="https://jquery-xml2json-plugin.googlecode.com/svn/trunk/jquery.xml2json.js"></script>
<script type="text/javascript">
$(function() {
var
xml,
json;
xml =
'<?xml version="1.0" encoding="utf-8"?>' +
'<catalog>' +
'<merchant id="1">' +
'<domain>http://www.amazon.com</domain>' +
'<affiliate>1</affiliate>' +
'</merchant>' +
'</catalog>';
json = $.xml2json(xml);
alert(json.merchant.domain);
});
</script>
var affiliateData = {};
function loadUrlData(url, data, affiliateData) {
var urlDomain = url.substring(url.indexOf('//') + 2);
if (urlDomain.indexOf('/') > -1) {
urlDomain = urlDomain.substring(0, urlDomain.indexOf('/'));
}
if (urlDomain.indexOf('?') > -1) {
urlDomain = urlDomain.substring(0, urlDomain.indexOf('?'));
}
lastIndexOfDot = urlDomain.lastIndexOf('.');
if (lastIndexOfDot < 0) {
return;
}
do {
affiliateData[urlDomain] = data;
urlDomain = urlDomain.slice(urlDomain.indexOf('.') + 1);
} while (urlDomain.indexOf('.') > -1);
};
function getAffiliateData(url, affiliateData) {
var urlDomain = url.substring(url.indexOf('//') + 2);
if (urlDomain.indexOf('/') > -1) {
urlDomain = urlDomain.substring(0, urlDomain.indexOf('/'));
}
if (urlDomain.indexOf('?') > -1) {
urlDomain = urlDomain.substring(0, urlDomain.indexOf('?'));
}
lastIndexOfDot = urlDomain.lastIndexOf('.');
if (lastIndexOfDot < 0) {
return 'none';
}
do {
for (domain in affiliateData) {
if (domain == urlDomain) {
return affiliateData[domain];
}
}
urlDomain = urlDomain.slice(urlDomain.indexOf('.') + 1);
} while (urlDomain.indexOf('.') > -1);
return 'none';
};
var url = 'http://www.amazon.com?asdf=/a/b/c';
var url2 = 'http://www.yahoo.com?asdf=/a/b/c';
loadUrlData('http://www.amazon.com?asdf=/a/b/c', 'data amazon', affiliateData);
loadUrlData('http://www.yahoo.com?asdf=/a/b/c', 'data yahoo', affiliateData);
alert(getAffiliateData(url, affiliateData));
alert(getAffiliateData(url2, affiliateData));
相关文章:
- 如何根据数值通过数据属性进行搜索并获得最接近的值
- 在JavaScript中编译搜索后,获取链接到函数的属性标记的Element
- 搜索具有特定属性集的非嵌套节点
- 在 XML 文件中搜索 TestComplete 中的特定属性值
- 使用 CSS3 数据属性进行客户端文本搜索
- 如何在 XML 中搜索特定属性以检索另一个属性
- 在 xml 中搜索属性
- Lodash搜索对象属性并修改值
- Lodash关于使用搜索词和多个属性名称进行筛选的帮助
- 如何在对象中搜索属性
- 遍历HTML结构以搜索属性
- 使用变量搜索窗口中的属性使用'在'中;
- Javascript Regex exec 在向搜索文本添加属性/引号后冻结
- 如何通过 jquery 通配符搜索数据属性的精确包含值
- 自动完成功能,用于按属性搜索 json 对象
- 如何通过某些属性搜索并返回knockout视图模型的元素
- 创建一个字典并按属性搜索
- 使用属性搜索json对象并获取该对象的所有相应属性
- 基于数据属性搜索html元素
- 使用特定属性搜索关注者