title: PARSING A COMPLEX XML USING DOM
body: <p>I know that this sort of question has been asked here before, but still i couldn't find any solution to my problem. So please, can anyone help me with it.....</p>
  
  <p><strong>Situation:</strong> </p>
  
  <p>I am parsing the following xml response using DOM parser.</p>
  
  <pre><code>&lt;feed&gt;
  &lt;post_id&gt;16&lt;/post_id&gt;
  &lt;user_id&gt;68&lt;/user_id&gt;
  &lt;restaurant_id&gt;5&lt;/restaurant_id&gt;
  &lt;dish_id&gt;7&lt;/dish_id&gt;
  &lt;post_img_id&gt;14&lt;/post_img_id&gt;
  &lt;rezing_post_id&gt;&lt;/rezing_post_id&gt;
  &lt;price&gt;8.30&lt;/price&gt;
  &lt;review&gt;very bad&lt;/review&gt;
  &lt;rating&gt;4.0&lt;/rating&gt;
  &lt;latitude&gt;22.299999000000&lt;/latitude&gt;
  &lt;longitude&gt;73.199997000000&lt;/longitude&gt;
  &lt;near&gt; Gujarat&lt;/near&gt;
  &lt;posted&gt;1340869702&lt;/posted&gt;
  &lt;display_name&gt;username&lt;/display_name&gt;
  &lt;username&gt;vivek&lt;/username&gt;
  &lt;first_name&gt;vivek&lt;/first_name&gt;
  &lt;last_name&gt;mitra&lt;/last_name&gt;
  &lt;dish_name&gt;Hash brows&lt;/dish_name&gt;
  &lt;restaurant_name&gt;Waffle House&lt;/restaurant_name&gt;
  &lt;post_img&gt;https://img1.yumzing.com/1000/9cab8fc91&lt;/post_img&gt;
  &lt;post_comment_count&gt;0&lt;/post_comment_count&gt;
  &lt;post_like_count&gt;0&lt;/post_like_count&gt;
  &lt;post_rezing_count&gt;0&lt;/post_rezing_count&gt;
  &lt;comments&gt;
  &lt;comment/&gt;
  &lt;/comments&gt;
  &lt;/feed&gt;
  
  &lt;feed&gt;
  &lt;post_id&gt;8&lt;/post_id&gt;
  &lt;user_id&gt;13&lt;/user_id&gt;
  &lt;restaurant_id&gt;5&lt;/restaurant_id&gt;
  &lt;dish_id&gt;6&lt;/dish_id&gt;
  &lt;post_img_id&gt;7&lt;/post_img_id&gt;
  &lt;rezing_post_id&gt;&lt;/rezing_post_id&gt;
  &lt;price&gt;3.50&lt;/price&gt;
  &lt;review&gt;This is cheesy!&lt;/review&gt;
  &lt;rating&gt;4.0&lt;/rating&gt;
  &lt;latitude&gt;42.187000000000&lt;/latitude&gt;
  &lt;longitude&gt;-88.346497000000&lt;/longitude&gt;
  &lt;near&gt;Lake in the Hills IL&lt;/near&gt;
  &lt;posted&gt;1340333509&lt;/posted&gt;
  &lt;display_name&gt;username&lt;/display_name&gt;
  &lt;username&gt;Nullivex&lt;/username&gt;
  &lt;first_name&gt;Bryan&lt;/first_name&gt;
  &lt;last_name&gt;Tong&lt;/last_name&gt;
  &lt;dish_name&gt;Hash Brows with Cheese&lt;/dish_name&gt;
  &lt;restaurant_name&gt;Waffle House&lt;/restaurant_name&gt;
  &lt;post_img&gt;https://img1.yumzing.com/1000/78e5c184fd3ae752f8665636381a8f0006762dc0&lt;/post_img&gt;
  &lt;post_comment_count&gt;6&lt;/post_comment_count&gt;
  &lt;post_like_count&gt;1&lt;/post_like_count&gt;
  &lt;post_rezing_count&gt;1&lt;/post_rezing_count&gt;
  &lt;comments&gt;
  &lt;comment&gt;
  &lt;user_id&gt;16&lt;/user_id&gt;
  &lt;email&gt;existentialism27@gmail.com&lt;/email&gt;
  &lt;email_new&gt;&lt;/email_new&gt;
  &lt;email_verification_code&gt;&lt;/email_verification_code&gt;
  &lt;password&gt;9d99ef4f72f9d2df968a75e058c78245fa45e9e7&lt;/password&gt;
  &lt;password_reset_code&gt;&lt;/password_reset_code&gt;
  &lt;salt&gt;31a988badccd35a1be7dacc073f60f52e49ff881&lt;/salt&gt;
  &lt;username&gt;existentialism27&lt;/username&gt;
  &lt;first_name&gt;Daniel&lt;/first_name&gt;
  &lt;last_name&gt;Amaya&lt;/last_name&gt;
  &lt;display_name&gt;username&lt;/display_name&gt;
  &lt;birth_month&gt;10&lt;/birth_month&gt;
  &lt;birth_day&gt;5&lt;/birth_day&gt;
  &lt;birth_year&gt;1985&lt;/birth_year&gt;
  &lt;city&gt;Colorado Springs&lt;/city&gt;
  &lt;state&gt;CO&lt;/state&gt;
  &lt;country&gt;US&lt;/country&gt;
  &lt;timezone&gt;US/Mountain&lt;/timezone&gt;
  &lt;last_seen&gt;1338365509&lt;/last_seen&gt;
  &lt;is_confirmed&gt;1&lt;/is_confirmed&gt;
  &lt;is_active&gt;1&lt;/is_active&gt;
  &lt;post_comment_id&gt;9&lt;/post_comment_id&gt;
  &lt;post_id&gt;8&lt;/post_id&gt;
  &lt;comment&gt;this is a test comment!&lt;/comment&gt;
  &lt;posted&gt;1340334121&lt;/posted&gt;
  &lt;/comment&gt;
  
  &lt;comment&gt;
  &lt;user_id&gt;16&lt;/user_id&gt;
  &lt;email&gt;existentialism27@gmail.com&lt;/email&gt;
  &lt;email_new&gt;&lt;/email_new&gt;
  &lt;email_verification_code&gt;&lt;/email_verification_code&gt;
  &lt;password&gt;9d99ef4f72f9d2df968a75e058c78245fa45e9e7&lt;/password&gt;
  &lt;password_reset_code&gt;&lt;/password_reset_code&gt;
  &lt;salt&gt;31a988badccd35a1be7dacc073f60f52e49ff881&lt;/salt&gt;
  &lt;username&gt;existentialism27&lt;/username&gt;
  &lt;first_name&gt;Daniel&lt;/first_name&gt;
  &lt;last_name&gt;Amaya&lt;/last_name&gt;
  &lt;display_name&gt;username&lt;/display_name&gt;
  &lt;birth_month&gt;10&lt;/birth_month&gt;
  &lt;birth_day&gt;5&lt;/birth_day&gt;
  &lt;birth_year&gt;1985&lt;/birth_year&gt;
  &lt;city&gt;Colorado Springs&lt;/city&gt;
  &lt;state&gt;CO&lt;/state&gt;
  &lt;country&gt;US&lt;/country&gt;
  &lt;timezone&gt;US/Mountain&lt;/timezone&gt;
  &lt;last_seen&gt;1338365509&lt;/last_seen&gt;
  &lt;is_confirmed&gt;1&lt;/is_confirmed&gt;
  &lt;is_active&gt;1&lt;/is_active&gt;
  &lt;post_comment_id&gt;10&lt;/post_comment_id&gt;
  &lt;post_id&gt;8&lt;/post_id&gt;
  &lt;comment&gt;this is a test comment!&lt;/comment&gt;
  &lt;posted&gt;1340334128&lt;/posted&gt;
  &lt;/comment&gt;
  &lt;/comments&gt;
  &lt;/feed&gt;
  </code></pre>
  
  <p>In the above xml response, i am <strong>getting multiple "feed", which i am able to parse without any problem</strong>, but here <strong>each "feed" can have None or N numbers of "comment"</strong>. I am <strong>not able to parse the comment for an individual feed</strong>. Can anyone suggest me how do proceed in the right direction.</p>
  
  <p>I am <strong>also putting a snippet of code here, NOT the entire code.. that i am using to parse the xml doc</strong>, so it will be easier to pin point the problem.</p>
  
  <pre><code>DocumentBuilderFactory odbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder odb =  odbf.newDocumentBuilder();
  InputSource is = new InputSource(new StringReader(xml));
  Document odoc = odb.parse(is);
  odoc.getDocumentElement().normalize ();    
  NodeList LOP = odoc.getElementsByTagName("feed");
  System.out.println(LOP.getLength());
  
    for (int s=0 ; s&lt;LOP.getLength() ; s++){
  
         Node FPN =LOP.item(s);
         try{
              if(FPN.getNodeType() == Node.ELEMENT_NODE)
                  {
  
              Element token = (Element)FPN;
  
              NodeList oNameList0 = token.getElementsByTagName("post_id");
              Element ZeroNameElement = (Element)oNameList0.item(0);
              NodeList textNList0 = ZeroNameElement.getChildNodes();
  
  feed_post_id = Integer.parseInt(((Node)textNList0.item(0)).getNodeValue().trim());
  
   System.out.println("#####The Parsed data#####");
   System.out.println("post_id : " + ((Node)textNList0.item(0)).getNodeValue().trim());
   System.out.println("#####The Parsed data#####");
  
              }
             }catch(Exception ex){}
               }
  </code></pre>