合理简单。首先,您解析 xml。实际上,我已经整合了一个 X 浏览器功能,可以很好地满足您的需求。你可以在这里找到它
取消注释这一行:
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