JavaScript XML DOM parser - parsing XML using Javascript and set values to HTML elements

Use following Code to parse the XML string in Javascript


function generateTextBoxValue(sourceXMLStr){

var expectedXML;
if (window.DOMParser){
var parser=new DOMParser();
expectedXML=parser.parseFromString(decodeURIComponent(sourceXMLStr),"text/xml");
  //decodeURIComponent is used assuming sourceXMLStr is UTF-8 encoded otherwise call directly
}else {// Internet Explorer
expectedXML=new ActiveXObject("Microsoft.XMLDOM");
expectedXML.async="false";
expectedXML.loadXML(decodeURIComponent(sourceXMLStr));
}

var testModuleList=expectedXML.getElementsByTagName(TEST_XML_MODULE_ELEMENT_NAME);// constant contains testModule XML Element name

for(var x=1;x<=testModuleList.length;x++){
document.getElementById(testTextBox).value = testModuleList[x-1].childNodes[0].nodeValue;
//fill your DOM elements based on your criteria
}

}

No comments:

Post a Comment