Iterate over DOM Nodelist

I have an XML in the form of
<feedback>
    <fb_info>
        <fb_id>27006971</fb_id>
    </fb_info>
    <fb_responses>
        <fb_input_approve>
            <res_id>123</res_id>
        </fb_input_approve>
        <fb_response>
            <res_id>345</res_id>
        </fb_response>
    </fb_responses>
</feedback>
I want to iterate over all the elements inside fb_responses, so that whenever a tag is encountered like fb_input_approve or fb_response, I can do some action.
This is what I am doing (attached code snippet)
But in the nodelist, I am getting only one element.

Can someone please let me know how to achieve this using DOM

NodeList responsesNodeList = document.getElementsByTagName("fb_responses");
       System.out.println("resposne node list lenght :  "+responsesNodeList.getLength());
       for(int i=0; i<responsesNodeList.getLength(); i++) {
           Node childOfResponses = responsesNodeList.item(i);
           if (childOfResponses.getNodeType() == childOfResponses.ELEMENT_NODE) {
                   String strChildOfResponses = childOfResponses.getNodeName();
                   System.out.println(childOfResponses.getNodeName()+"  :  "+childOfResponses.getNodeValue());
                   
                   if(strChildOfResponses.equalsIgnoreCase("fb_input_approve")) {
           IXMLCreator cfxml = new CreateApproverXML();
           StringBuffer xmlChunck = cfxml.getXMLChunck(response_id, session);
                                      
                 
                   } 
           }
       }

Open in new window

caroline_123Asked:
Who is Participating?
 
MortenSlotKristensenCommented:
In the XML you only have one <fb_responses> so thats why you only get one element. :)
0
 
MortenSlotKristensenCommented:
Which XMLReader are you using by the way?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.