/*
* get all nodes's attribute and text
*/
function getAT(nodes){
var XMLData=[];
for(var i=0;i<nodes.length;i++){
var crtNode={};
crtNode.$name=nodes[i].nodeName;
if(nodes[i].attributes){
for(var j=0;j<nodes[i].attributes.length;j++){
debug.innerHTML+='<i>'+nodes[i].attributes[j].name+'='+nodes[i].attributes[j].value+'</i><br>';
crtNode[nodes[i].attributes[j].name]=nodes[i].attributes[j].value;
}
}
if(nodes[i].hasChildNodes()){
if(nodes[i].firstChild.nodeType==3){
crtNode.$value=nodes[i].firstChild.nodeValue;
}else{
crtNode.$value=null;
var childNode=getAT(nodes[i].childNodes);
debug.innerHTML+='<u>'+childNode[0].$name+'</u><br>';
crtNode[childNode[0].$name]=childNode;
}
}
XMLData.push(crtNode);
}
return XMLData;
}
/*
* Load XML into an array, each item of this array is an object. every object has at least two property: $name which is the xml node name and $value which is the text of the xml node(maybe null).
* @usage:
xml=loadXML('game.xml');
*/
var XMLMSG='';// record error message, when a parse error ouccored
var _XML=''; // save xml text
var loadXML = function(xmlFile)
{
var xmlDoc;
if(window.ActiveXObject)
{
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = false;
xmlDoc.load(xmlFile);
}
else if (document.implementation&&document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument('', '', null);
xmlDoc.load(xmlFile);
}
else
{
XMLMSG='Sorry, your browser doesn't support XML.'
return false;
}
if(xmlDoc.parseError.errorCode!=0){
XMLMSG+="<br/>Error Code: ";
XMLMSG+=xmlDoc.parseError.errorCode;
XMLMSG+="<br/>Error Reason: ";
XMLMSG+=xmlDoc.parseError.reason;
XMLMSG+="<br/>Error Line: ";
XMLMSG+=xmlDoc.parseError.line;
return false;
}
_XML=xmlDoc.documentElement.xml;
var xmlNodes=xmlDoc.documentElement.childNodes;
debug.innerHTML+='<hr><font color="#ff0000">';
return getAT(xmlNodes);
}
* get all nodes's attribute and text
*/
function getAT(nodes){
var XMLData=[];
for(var i=0;i<nodes.length;i++){
var crtNode={};
crtNode.$name=nodes[i].nodeName;
if(nodes[i].attributes){
for(var j=0;j<nodes[i].attributes.length;j++){
debug.innerHTML+='<i>'+nodes[i].attributes[j].name+'='+nodes[i].attributes[j].value+'</i><br>';
crtNode[nodes[i].attributes[j].name]=nodes[i].attributes[j].value;
}
}
if(nodes[i].hasChildNodes()){
if(nodes[i].firstChild.nodeType==3){
crtNode.$value=nodes[i].firstChild.nodeValue;
}else{
crtNode.$value=null;
var childNode=getAT(nodes[i].childNodes);
debug.innerHTML+='<u>'+childNode[0].$name+'</u><br>';
crtNode[childNode[0].$name]=childNode;
}
}
XMLData.push(crtNode);
}
return XMLData;
}
/*
* Load XML into an array, each item of this array is an object. every object has at least two property: $name which is the xml node name and $value which is the text of the xml node(maybe null).
* @usage:
xml=loadXML('game.xml');
*/
var XMLMSG='';// record error message, when a parse error ouccored
var _XML=''; // save xml text
var loadXML = function(xmlFile)
{
var xmlDoc;
if(window.ActiveXObject)
{
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = false;
xmlDoc.load(xmlFile);
}
else if (document.implementation&&document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument('', '', null);
xmlDoc.load(xmlFile);
}
else
{
XMLMSG='Sorry, your browser doesn't support XML.'
return false;
}
if(xmlDoc.parseError.errorCode!=0){
XMLMSG+="<br/>Error Code: ";
XMLMSG+=xmlDoc.parseError.errorCode;
XMLMSG+="<br/>Error Reason: ";
XMLMSG+=xmlDoc.parseError.reason;
XMLMSG+="<br/>Error Line: ";
XMLMSG+=xmlDoc.parseError.line;
return false;
}
_XML=xmlDoc.documentElement.xml;
var xmlNodes=xmlDoc.documentElement.childNodes;
debug.innerHTML+='<hr><font color="#ff0000">';
return getAT(xmlNodes);
}
参考文章:
javascript解析XML的方法 作者:luke 日期:2007-05-31 URL http://www.lukee.cn/article.asp?id=396