how to read xml file with many nodes with javascript -
i have registrationresponsemessages.xml:
<messages> <error> <code id="501">couldn't retrieve html document because of server-configuration problems.</code> <code id="502">server busy, site may have moved ,or lost dial-up internet connection.</code> </error> <success></success> </messages>
trying read contents of code id 501 , 502 javascript, not works.
if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari xmlhttp = new xmlhttprequest(); } else {// code ie6, ie5 xmlhttp = new activexobject("microsoft.xmlhttp"); } xmlhttp.open("get", "registrationresponsemessages.xml", false); xmlhttp.send(); xmldoc = xmlhttp.responsexml; document.getelementbyid("errorcode403").innerhtml = getelementsbytagname(501)[0].childnodes[0].nodevalue);
displaying here:
<label id="errorcode403" style="font-weight: 600; color: red;">give error</label>
what problem?
it's ajax, have wait data returned, have access right way:
var xmlhttp; if (window.xmlhttprequest) { xmlhttp = new xmlhttprequest(); } else { xmlhttp = new activexobject("microsoft.xmlhttp"); } xmlhttp.onload = function() { var xmldoc = this.responsexml, value = xmldoc.getelementsbytagname('501')[0].childnodes[0].nodevalue; document.getelementbyid("errorcode403").innerhtml = value; } xmlhttp.open("get", "registrationresponsemessages.xml", false); xmlhttp.send();
not sure traversal in xml, 501
sounds strange tagname ?
edit:
to list of id's inside onload handler:
xmlhttp.onload = function() { var xmldoc = this.responsexml, var codes = xmldoc.getelementsbytagname('code'); var array = []; (var i=0; i<codes.length; i++) { array.push( codes[i].id ); } console.log(array); }
Comments
Post a Comment