這很合理簡單。首先,你解析XML。實際上,我已經將X瀏覽器功能放在了一起,非常適合您的需求。您可以find it here
取消註釋該行:
keep.push(parsed);//last element of array is the full DOM
只是把它就像這樣:
var data1 = parseXML(xml1,'RNA','x');//xml1 is the first xml string
var data2 = parseXML(xml2,'RNA','x');//xml2 === second xml string
var xml1Parsed = data1.splice(-1);//assign document to variable
var xml2Parsed = data2.splice(-1);
//get the nodes of intrest
var RNA1 = xml1Parsed.getElementsByTagName('RNA');
var RNA2 = xml2Parsed.getElementsByTagName('RNA');
兩個data1
和data2
現在陣列,只是在它們之間迭代:
for (var i=0;i<data1.length;i++)
{
if (data1[i] === data2[i])//attribute x is the same
{//check whatever you need
if (RNA1[i].getAttribute('y') === RNA2[i].getAttribute('y'))
{
console.log('node ' + i + ' is alike');//do stuff
}
}
}
爲了完整和清晰,這是功能作爲本例中使用:
var parseXML = (function(w,undefined)
{
'use strict';
var parser,i,ie,parsed;
ie = false;
switch (true)
{
case w.DOMParser !== undefined:
parser = new w.DOMParser();
break;
case new w.ActiveXObject("Microsoft.XMLDOM") !== undefined:
parser = new w.ActiveXObject("Microsoft.XMLDOM");
parser.async = false;
ie = true;
break;
default :
throw new Error('No parser found');
}
return function(xmlString,getTags,getAttr)
{
var tags,keep = [];
if (ie === true)
{
parser.loadXML(xmlString);
parsed = parser;
}
else
{
parsed = parser.parseFromString(xmlString,'text/xml');
}
tags = parsed.getElementsByTagName(getTags);
for(i=0;i<tags.length;i++)
{
if (!getAttr || getAttr === 'textContent')
{
keep.push(tags[i].textContent);
continue;
}
if (getAttr && tags[i].getAttribute(getAttr))
{
keep.push(tags[i].getAttribute(getAttr));
}
}
keep.push(parsed);//last element of array is the full DOM
return keep;
}
})(this);//assuming global declaration, if not: replace with window
不清楚你真的想要比較 – wroniasty
我很抱歉,如果我不清楚,我只需要比較'x'屬性值彼此,如果匹配,則獲得'x'屬性值。 – user1495475