Home >Web Front-end >JS Tutorial >javascript reads xml_javascript tips

javascript reads xml_javascript tips

WBOY
WBOYOriginal
2016-05-16 19:24:371116browse

Copy code The code is as follows:
/**
* Get the collection object of XML file attributes
* @param xmlDoc XML object
* @param name Attribute name such as: user
* @return Return Array object
* Example XML:
*
*
*
*
* < ;cnname>Little Pig  
*                                      🎜> *                                                       * < /user>
*

*/
function getXMLArray(xmlDoc, name) {
var keys = name.split('.');
var node = xmlDoc.documentElement; // get Root node
var rtn = new Array();
var n = 0;

for(var i=0; i var children = node .childNodes; // Get child nodes
var key = keys[i];
for(var k=0; k if(child.nodeName == key) { // Determine whether the child node matches
if(i == keys.length-1) {
rtn[n] = child;
n ;
} else {
node = child;
break;
}
}
}
}


return rtn;
}


/**
* Get the value in the object obtained by the getXMLArray function
* @param node Node object
* @param name
* @return Return String
*/
function getValue(node, name) {
var keys = name.split('.');

for( var i=0; i var childs = node.childNodes; // Get child nodes
var key = keys[i];
for(var k=0; k node = child ;
break;
}
}
}
}


return "";




// 测试: 


var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
xmlDoc.async = false; 
xmlDoc.loadXML("小猪27小霞26小章25"); 


var list = getXMLArray(xmlDoc, 'user'); 
for(var i=0; i  var obj = list[i]; 
  document.write(getValue(obj, 'name.cnname')); 
  document.write(getValue(obj, 'age')); 
  document.write('
'); 
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn