0

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;
        }
    }

2 Answers 2

1

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
      }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

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
} );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.