I am dealing with an XML file that contains multiple instances of the same element name, as shown below. I am currently only able to retrieve the first occurrence and would like to know how to access all 4 "comments" using JavaScript. Thank you!
<FRIENDSSTATUS>
<STATUSFRIENDS id="2">
<STATUS>Life begins at the end of your comfort zone</STATUS>
<DATETIME>2012-10-30 10:32:28</DATETIME>
<FIRSTNAME>Malcolm</FIRSTNAME>
<LASTNAME>Landgraab</LASTNAME>
<COMMENTS datetime="2012-11-18 22:13:28" firstname="Ian" lastname="Tellerman">rr</COMMENTS>
<COMMENTS datetime="2012-11-18 22:13:39" firstname="Ian" lastname="Tellerman">hello</COMMENTS>
<COMMENTS datetime="2012-11-19 14:36:24" firstname="Ian" lastname="Tellerman">test</COMMENTS>
<COMMENTS datetime="2012-11-19 14:45:52" firstname="Ian" lastname="Tellerman">test4</COMMENTS>
</STATUSFRIENDS>
<STATUSFRIENDS id="3">
<STATUS>Prometheus. Fantastic film!!!!</STATUS>
<DATETIME>2012-10-30 10:32:28</DATETIME>
<FIRSTNAME>Mansa</FIRSTNAME>
<LASTNAME>Bendett</LASTNAME>
<COMMENTS datetime="2012-11-18 22:14:46" firstname="Ian" lastname="Tellerman">wrong</COMMENTS>
</STATUSFRIENDS>
</FRIENDSSTATUS>
Below are the javascript codes for retrieving the data:
xmlDoc = xmlhttp.responseXML;
$table = "<table border='1'>";
var element = xmlDoc.getElementsByTagName("FRIENDSSTATUS");
var elements = xmlDoc.getElementsByTagName("STATUSFRIENDS");
var comments = xmlDoc.getElementsByTagName("COMMENTS");
if(xmlDoc.documentElement !== null){
var root;
if (navigator.userAgent.indexOf("Firefox") > 0)
var root = element[0].childElementCount;
if (navigator.userAgent.indexOf("MSIE") > 0)
var root = xmlDoc.documentElement.childNodes.length;
for(i=0; i<root; i++){
$strStats = "";
$strComments = "";
$strDateTime = "";
$strFirstname = "";
$strLastname = "";
$strCommentDateTime = "";
$strCommentFirstName = "";
$strCommentLastName = "";
$strStatusID = "";
if(navigator.userAgent.indexOf("Firefox") > 0){
for(y=0;y < elements[i].childElementCount;y++){
$strStatusID = elements[i].getAttribute("id");
if(strStatus = elements[i].getElementsByTagName("STATUS")){
$strStats = strStatus[0].firstChild.nodeValue;
}
if(strStatus = elements[i].getElementsByTagName("DATETIME")){
$strDateTime = strStatus[0].firstChild.nodeValue;
}
if(strStatus = elements[i].getElementsByTagName("FIRSTNAME")){
$strFirstname = strStatus[0].firstChild.nodeValue;
}
if(strStatus = elements[i].getElementsByTagName("LASTNAME")){
$strLastname = strStatus[0].firstChild.nodeValue;
}
if(strComm = elements[i].getElementsByTagName("COMMENTS")){
$strComments = strComm[0].firstChild.nodeValue;
$strCommentDateTime = comments[i].getAttribute("datetime");
$strCommentFirstName = comments[i].getAttribute("firstname");
$strCommentLastName = comments[i].getAttribute("lastname");
$strComments += "<br>" + $strCommentFirstName + " " + $strCommentLastName + " " + $strCommentDateTime + "</br></br>";
}
$strStats = $strStats + " " + $strLastname + " " + $strFirstname + " " + $strDateTime;
$strFullName = $strFirstname + " " + $strLastname;
$strDateTime = "";
$strFirstname = "";
$strLastname = "";
}
}