Javascript loads XML to an array

Source: Internet
Author: User
Tags parse error

 

 

/**//*
* 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.