title: How to navigate dom tree from XML in java
body: <p>Below is a sample of the xml I am using, I have striped out some of the fields as they are unnecessary to demonstrate my point.  </p>
  
  <p>I am trying to parse the orders from this xml. However, I encounter a problem when I try to parse the product sets for each order.  When the first order is processing, instead of adding the 2 sets detailed below, it will add <strong>all</strong> the sets it can find in the xml into the first order.  I am not sure how to get around this as this is all quite new to me.  Below is my java...</p>
  
  <pre><code>DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
  Document doc = dBuilder.parse(xmlFile);
  doc.getDocumentElement().normalize();
  
  // Create a list of orders and sub elements
  System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
  nList = doc.getElementsByTagName("order");
  setList = doc.getElementsByTagName("set");
  orders = new Order[nList.getLength()];
  Node nNode = nList.item(i);
  if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  
      Element eElement = (Element) nNode;
      temp = new Order();
      // Populate order with details from XML
      parseClientDetails(eElement);
  
      // Add sets
      parseSets();
      temp.setSets(setArray);
      orders[i] = temp;
  }
  </code></pre>
  
  <p>...</p>
  
  <pre><code>    private void parseSets() {
          Node nNode;
          Element element;
          for (int c = 0; c &lt; setList.getLength(); c++) {
              nNode = setList.item(c);
              element = (Element) nNode;
  
              tempSet = new Set();
              tempSet.setBandwidth(getValue("bandwidth", element));
              tempSet.setCategory(getValue("category", element));
              tempSet.setSet_package(getValue("package", element));
              setArray.add(tempSet);
          }
      }
  </code></pre>
  
  <p>XML:</p>
  
  <pre><code>&lt;orderSet&gt;
    &lt;order&gt;
      &lt;customer name="SelectCustomerDetails"&gt;
          &lt;clientId&gt;UK12345&lt;/clientId&gt;
          &lt;etc&gt;...&lt;/etc&gt;
      &lt;/customer&gt;
      &lt;product&gt;
          &lt;set&gt;
              &lt;category&gt;Silver&lt;/category&gt;
              &lt;package&gt;3000IP&lt;/package&gt;
              &lt;bandwidth&gt;160&lt;/bandwidth&gt;
          &lt;/set&gt;
          &lt;set&gt;
              &lt;category&gt;Silver&lt;/category&gt;
              &lt;package&gt;3000IP&lt;/package&gt;
              &lt;bandwidth&gt;320&lt;/bandwidth&gt;
          &lt;/set&gt;
      &lt;/product&gt;
    &lt;/order&gt;
    &lt;order&gt;
    ...
    &lt;/order&gt;
  &lt;/orderSet&gt;
  </code></pre>