title: Write more than one element with same name to xml file using JDOM in JAVA
body: <p>I have to loop through an object containing the data for all the xml elements, set the values accordingly and write it into a file.</p>
  
  <p>Here is my source code,</p>
  
  <pre><code>for(int i=0;i&lt; dataHolder.getNumSensorsList().size();i++) {
  
      NumSensor numSensor = (NumSensor) dataHolder.getNumSensorsList().get(i);
      Element numSensorElement = new Element("numSensor");
      numSensorElement.setAttribute("ruleid",numSensor.getRuleId());
  
      Element type = new Element("type");
      type.addContent(numSensor.getUnit());
      numSensorElement.addContent(type);
  
      Element sensorId = new Element("sensorId");
      sensorId.addContent(numSensor.getSensorId());
      numSensorElement.addContent(sensorId);
  
      Element label = new Element("label");
      label.addContent(numSensor.getLabel());
      numSensorElement.addContent(label);
  }
  </code></pre>
  
  <p>In case if am looping for three times and adding different data to "<code>numSensorElement</code>" all three times and I add the "<code>numSensorElement</code>" to a root tag and write it into a xml file. I am getting three copies of "<code>numSensorElement</code>" with the information from third loop. First two loops are replaced and written with third loop values for three times.</p>
  
  <p>Please help me with some better approach than mine.</p>
  
  <p>Thanks in advance.</p>