/**//*
* 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;
}
_Xml1_xmldoc.doc umentelement. xml;
VaR xmlnodesstrap xmldoc.doc umentelement. childnodes;
Debug. innerhtml + = '<HR> <font color = "# ff0000"> ';
Return getat (xmlnodes );
}
References:
Method of parsing XML in Javascript Author: Luke Date: URL http://www.lukee.cn/article.asp? Id = 396