var xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"+
"<cm><cNname>1</cNname><cIP>12</cIP><db keyd='keyValue'><address idd='5'>192.168.1.101:27017</address><address>192.168.1.102:27017</address></db></cm>";node
//解析xml
function xmld()
{
var domParser = new DOMParser();
var xmlDoc = domParser.parseFromString(xmlString, 'text/xml');
var elem = xmlDoc.documentElement.childElementCount;
for(var i=0;i<elem;i++)
{
//判斷是否存在子節點若存在ifSon不爲0
var ifSon = xmlDoc.documentElement.childNodes[i].childElementCount;
var els = xmlDoc.firstChild.childNodes[i].nodeName;
var elsd = xmlDoc.firstChild.childNodes[i].innerHTML;
if(ifSon != 0)
{
var elsds = xmlDoc.documentElement.childNodes[i].outerHTML;
var xmlList = domParser.parseFromString(elsds, 'text/xml');
//獲取屬性keyd的值 keyValue
alert(xmlList.documentElement.attributes.keyd.nodeValue);
var listSize = xmlList.documentElement.childElementCount;
for(var j=0;j<listSize;j++)
{
alert(xmlList.firstChild.childNodes[j].attributes.idd.nodeValue);
var listName = xmlList.firstChild.childNodes[j].nodeName;
var listXalue = xmlList.firstChild.childNodes[j].innerHTML;
alert(listName+" "+listXalue);
}
}
else
{
alert(els+" "+elsd);
}
}
}dom