title: Java for loop to obtain child elements from xml document
body: <p>I'm getting hard time in figuring how to write a loop to retrieve values from a given XML document for simplicity I'll give you a  sample of the <code>xml</code> file </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;/solution&gt;
            &lt;solution&gt;
                 &lt;cost&gt;505.9208295302417&lt;/cost&gt;
                 &lt;routes&gt;
                      &lt;route&gt;
                           &lt;driverId&gt;noDriver&lt;/driverId&gt;
                           &lt;vehicleId&gt;1_1&lt;/vehicleId&gt;
                           &lt;start&gt;0.0&lt;/start&gt;
                           &lt;act type="service"&gt;
                                &lt;serviceId&gt;5  &lt;/serviceId&gt;
                                &lt;arrTime&gt;109.9819741964403&lt;/arrTime&gt;
                                &lt;endTime&gt;119.9819741964403&lt;/endTime&gt;
                           &lt;/act&gt;
                           &lt;end&gt;229.9639483928806&lt;/end&gt;
                      &lt;/route&gt;
                      &lt;route&gt;
                           &lt;driverId&gt;noDriver&lt;/driverId&gt;
                           &lt;vehicleId&gt;3_1&lt;/vehicleId&gt;
                           &lt;start&gt;0.0&lt;/start&gt;
                           &lt;act type="service"&gt;
                                &lt;serviceId&gt;4  &lt;/serviceId&gt;
                                &lt;arrTime&gt;109.98190391287031&lt;/arrTime&gt;
                                &lt;endTime&gt;119.98190391287031&lt;/endTime&gt;
                           &lt;/act&gt;
                           &lt;act type="service"&gt;
                                &lt;serviceId&gt;2 &lt;/serviceId&gt;
                                &lt;arrTime&gt;119.98282618841856&lt;/arrTime&gt;
                                &lt;endTime&gt;129.98282618841856&lt;/endTime&gt;
                           &lt;/act&gt;
                           &lt;act type="service"&gt;
                                &lt;serviceId&gt;1 &lt;/serviceId&gt;
                                &lt;arrTime&gt;129.98372097890456&lt;/arrTime&gt;
                                &lt;endTime&gt;139.98372097890456&lt;/endTime&gt;
                           &lt;/act&gt;
                           &lt;act type="service"&gt;
                                &lt;serviceId&gt;3 &lt;/serviceId&gt;
                                &lt;arrTime&gt;139.9846432544528&lt;/arrTime&gt;
                                &lt;endTime&gt;149.9846432544528&lt;/endTime&gt;
                           &lt;/act&gt;
                           &lt;end&gt;259.9668316441239&lt;/end&gt;
                      &lt;/route&gt;
                 &lt;/routes&gt;
            &lt;/solution&gt;
       &lt;/solutions&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>so basically what I've in the code that I'll be showing you is to obtain value from only the node where there is cost = 505.9208295302417, please don't take into account this part of the code, so the next step was to retrieve <code>driverid``, vehicleid and</code> act , I know that there is a missing for loop but I don't know where to put it!! please can someone help. The desired output that i need to have is like for every <code>vehicleid</code> obtain list act values associated with it </p>
  
  <p>java code </p>
  
  <pre><code>        public static void main(String[] args) {
          try {
              int totalVehicle;
              totalVehicle = 2;
              File fXmlFile = new File("C:/Users/HP/Desktop/solution.xml");
              DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
              DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
              Document doc = dBuilder.parse(fXmlFile);
              doc.getDocumentElement().normalize();
              Double requiredCost = 505.9208295302417;
              System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
              // NodeList nList = doc.getElementsByTagName("route");
              System.out.println("----------------------------");
              NodeList nodeList = doc.getElementsByTagName("solution");
              for (int i = 0; i &lt; nodeList.getLength(); i++) {
  
                  Node solutionNode = nodeList.item(i);
  
                  if (solutionNode.getNodeType() == Node.ELEMENT_NODE) {
                      Element solutionElement = (Element) solutionNode;
                      Node costNode = solutionElement.getElementsByTagName("cost").item(0);
                      Node route = solutionElement.getElementsByTagName("routes").item(0);
                      // if correct cost, proceed to parse further
                      Double costValue = Double.valueOf(costNode.getTextContent());
                      if (Double.compare(requiredCost, costValue) == 0) {
                          System.out.println("working");
                          // there you go, found the node with the cost 505.9208295302417
                          // now just parse all the node elements you need here
  
                          System.out.println("cost : "
                                  + solutionElement.getElementsByTagName("cost")
                                          .item(0).getTextContent());
                          for (int h = 0; h &lt; totalVehicle; h++) {
                              System.out.println("DriverId : "
                                      + solutionElement.getElementsByTagName("driverId")
                                              .item(h).getTextContent().toString());
                              System.out.println("vehicleId : "
                                      + solutionElement.getElementsByTagName("vehicleId")
                                              .item(h).getTextContent());
  NodeList optionList = solutionElement.getElementsByTagName("act");
                              System.out.println(optionList.getLength());
  
          for (int j = 0; j &lt; optionList.getLength(); ++j)
          {
  
              for(int k =0;k&lt;1;++k){
                  Element option = (Element) optionList.item(j);
              String optionText = option.getTextContent();
             //address.add(optionText.replaceAll("[^A-Za-z]"," "));
              System.out.println("Citizen :"+optionText.replaceAll("[^A-Za-z]"," "));}
              ;
  
  
          }
  </code></pre>