I'm trying to parse some XML results in javascript to use with phonegap. As it stands my xml layout is:
<Results>
<Result>
<FirstName>John</FirstName>
<Surname>Beech</Surname>
<Company>CompanyName</Company>
<Job_Title>Property Department</Job_Title>
<UserID>184</UserID>
<CompanyID>CompanyID</CompanyID>
</Result>
<Result>
<FirstName>Rosie</FirstName>
<Surname>Beech</Surname>
<Company>CompanyName</Company>
<Job_Title>Job Title</Job_Title>
<UserID>10494</UserID>
<CompanyID>17322</CompanyID>
</Result>
</Results>
And I'm using the following javascript to at the moment just alert out the responses, but eventually I want to create a table of the responses.
<script language="javascript" type="text/javascript">
window.onload = function () {
$.ajax({
type: 'GET',
url: 'Lookupbysurname.aspx?surname=beech',
dataType: 'html',
success: function (data) {
try {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(data);
}
catch (e) {
try {
parser = new DOMParser();
xmlDoc = parser.parseFromString(data, "text/xml");
}
catch (e) {
alert(e.message);
return;
}
}
for (var i = 0; i < xmlDoc.getElementsByTagName("CompanyID")[0].childNodes[0].length; i++) {
alert(xmlDoc.getElementsByTagName("CompanyID")[0].childNodes[0].nodeValue);
}
}
});
}
</script>
However at the moment it's only alerting the same response out over and over. Have I put the loop together wrong? Loops in JS arent my forte! Any help will be appreciated.