?
Solved

Iterate over DOM Nodelist

Posted on 2008-11-08
2
Medium Priority
?
9,029 Views
Last Modified: 2013-12-29
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

0
Comment
Question by:caroline_123
  • 2
2 Comments
 
LVL 2

Accepted Solution

by:
MortenSlotKristensen earned 820 total points
ID: 22911804
In the XML you only have one <fb_responses> so thats why you only get one element. :)
0
 
LVL 2

Expert Comment

by:MortenSlotKristensen
ID: 22911855
Which XMLReader are you using by the way?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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 …
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

840 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