Iterate over DOM Nodelist

Posted on 2008-11-08
Last Modified: 2013-12-29
I have an XML in the form of
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

Question by:caroline_123
    LVL 2

    Accepted Solution

    In the XML you only have one <fb_responses> so thats why you only get one element. :)
    LVL 2

    Expert Comment

    Which XMLReader are you using by the way?

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now