所以我有一个XML文件,我使用javascript来解析并使用XMLHttpRequest转换为HTML。我想要做的是过滤掉链接标记中包含字符串英文的元素并保存到另一个数组中。这样我就可以添加一个limit参数并循环遍历新数组来控制页面上填充的元素数量。我对编码很陌生,并且我不太确定如何将responseXML过滤掉并放入另一个数组中,或者即使是最好的apprach也是如此。基本的XML文件结构和我的代码如下:
<channel>
<item>
<title>Test Title</title>
<link>https://www.test.com/English/rest-of-link</link>
<description>This is a dummy despcription</description>
<pubDate>Wed, 31 May 2017 17:52:51 GMT</pubDate>
</item>
<item>
<title>Test Title2</title>
<link>https://www.test.com/Spanish/rest-of-link</link>
<description>This is another dummy despcription</description>
<pubDate>Wed, 31 May 2017 17:52:51 GMT</pubDate>
</item>
</channel>
var myitems;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
printHtml(this, limit);
}
};
xhttp.open("GET", "rss.xml", true);
xhttp.send();
function printHtml(xml) {
var xmlDoc = xml.responseXML;
myitems = xmlDoc.getElementsByTagName('item');
/*********/
filter out items with the string English in the url into another array
/*********/
i = 0;
for(i=0;i<myitems.length;++i) {
var mylink = myitems[i].getElementsByTagName('link')[0].childNodes[0].nodeValue;
var html = '<a href="'+ mylink +'"><div class="home-entry-header"></a></div>';
$(".full-list").append(html);
}
}