Javascript 分析数据以创建导航窗格
我有以下XML响应: 我正在尝试使用转储中的信息创建类似的内容:Javascript 分析数据以创建导航窗格,javascript,jquery,xml,parsing,loops,Javascript,Jquery,Xml,Parsing,Loops,我有以下XML响应: 我正在尝试使用转储中的信息创建类似的内容: <UL> <li>Academic <ul> <li>BM</li> <li>CMTTE</LI> <li>DM</li> <li>PM</li> </ul> </li> </ul> <ul&
<UL>
<li>Academic
<ul>
<li>BM</li>
<li>CMTTE</LI>
<li>DM</li>
<li>PM</li>
</ul>
</li>
</ul>
<ul>
<li>ARCHIVE</li>
</UL>
<ul>
<LI>ASSOCIATIONS
<ul>
<li>BM</li>
<li>DM</LI>
<li>PM</li>
</ul>
</LI>
</ul>
我如何浏览这些信息并附加ul和li标签来创建站点导航菜单
JS用于获取XML:
function getAllSites(){
$().SPServices({
operation: "GetAllSubWebCollection",
async: true,
completefunc: function(xData, Status){
$(xData.responseXML).find("Web").each(function(){
console.log($(this).attr("Url"));
});
}
});
}
一个简单的解决方案是根据链接的深度构建索引图,深度由
url
中/
的数量决定
var map = {}; //init the map
for (var i = 0, l = webs.length; i < l; i++) {
//we create a index for our links based on the depth of them by `/`
var m = webs[i].attributes['Url'].value.substring(23, webs[i].attributes['Url'].value.length).split('/').length;
map[m] = map[m] || []; //make sure we leave alone the old values if there is none init with new array
map[m].push(webs[i].attributes['Url'].value); //push new value to node
}
console.log(map);
从这里,您可以创建元素列表。您让我非常接近我要做的事情,但您似乎教导我要将所有级别分组在一起。(也就是说,所有级别1的站点都在一起,所有级别2的站点都在一起,等等)这并不是我想要实现的目标。我更新了我的OP,让你明白我的意思
var map = {}; //init the map
for (var i = 0, l = webs.length; i < l; i++) {
//we create a index for our links based on the depth of them by `/`
var m = webs[i].attributes['Url'].value.substring(23, webs[i].attributes['Url'].value.length).split('/').length;
map[m] = map[m] || []; //make sure we leave alone the old values if there is none init with new array
map[m].push(webs[i].attributes['Url'].value); //push new value to node
}
console.log(map);
{
"1": ["https://hosted.demo.ca", "https://hosted.demo.ca/academic", "https://hosted.demo.ca/archive", ...],
"2": ["https://hosted.demo.ca/academic/bm", "https://hosted.demo.ca/academic/cmtte", ...],
}