Reputation: 17
I have my xml in this format :
<ops>
<emp>
<name>qwer</name>
<ntid>qwer</ntid>
<pwd>qwer</pwd>
<mailid>qwer</mailid>
<score>
<attempt>
<percentage>75</percentage>
<result>3</result>
<exam_name>db_creation_retirement</exam_name>
</attempt>
</score>
</emp>
<emp>
<name>asdf</name>
<ntid>asdf</ntid>
<pwd>asdf</pwd>
<mailid>asdf</mailid>
<score>
<attempt>
<percentage>75</percentage>
<result>3</result>
<exam_name>db_creation_retirement</exam_name>
</attempt>
<attempt>
<percentage>25</percentage>
<result>1</result>
<exam_name>db_creation_retirement</exam_name>
</attempt>
<attempt>
<percentage>50</percentage>
<result>2</result>
<exam_name>db_creation_retirement</exam_name>
</attempt>
</score>
</emp>
<ops>
i want the percentage, result and exam_name for all the attempts by a name in javascript.
here is my javascript i'm using and i'm struck up fetching the child node value.
i'm selecting the names and for a selected name i want the complete score tag to be displayed. please help me in getting this. the javascript im using is :
for ( var i = 0; i < xmlDoc.getElementsByTagName("name").length; i++) {
uname = xmlDoc.getElementsByTagName("name")[i].childNodes[0].nodeValue;
alert(uname);
if (selectedValue == uname) {
for ( var j = 0; j < xmlDoc.getElementsByTagName("score")[i].childNodes.length; j++) {
var attempt = xmlDoc.getElementsByTagName("score")[i].childNodes[j].nodeValue;
alert("in loop");
alert(attempt);
}
alert("name is equal");
break;
}
}
Upvotes: 0
Views: 1935
Reputation: 873
For given name retrieve it's score :
function getScoreByName(nameparam, xml){
var nameList = xml.getElementsByTagName('name');
for(var i=0; i<nameList.length; i++){
if(nameparam === nameList[i].firstChild.nodeValue){
//parentNode = <emp>
//lastChild = <score>
handleAttempts(nameList[i].parentNode.lastChild.childNodes);
//print name here
}
}
}
function handleAttempts(attemptList){
var percentage;
var result;
var examName;
for(var i=0; i<attemptList.length; i++){
percentage=attemptList[i].childNodes[0].firstChild.nodeValue;
result=attemptList[i].childNodes[1].firstChild.nodeValue;
examName=attemptList[i].childNodes[2].firstChild.nodeValue;
//print attempt here
}
}
Upvotes: 1
Reputation: 3753
Use jQuery , and its selectors e.g
elements = $(yourXml).find('name');
$.each(elements, function(key, elem) {
$(elem).find("score");
//do whatever you want with individual score nodes
} );
Upvotes: 0