title: Merging two xml files in one file
body: <p>I have written code in Java to merge two xml file, 
  the first file is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;dependency&gt;
  &lt;ownedtestcase ID="UT001_GetPatientInfo" Package="emrservicedesigntest" source="EMRService"&gt;
  &lt;cutdependency ID="EMRService" Package="emrservicedesign"/&gt;
  &lt;/ownedtestcase&gt;
  &lt;/dependency&gt;`  
  </code></pre>
  
  <p>The second file is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;dependency&gt;
  &lt;ownedtestcase ID="UT001_ScheduleTreatment" Package="emrservicedesigntest" source="EMRService"&gt;
  &lt;cutdependency ID="Symptom" Package="emrservicedesign"/&gt;
  &lt;cutdependency ID="PatientInfo" Package="emrservicedesign"/&gt;
  &lt;/ownedtestcase&gt;
  &lt;/dependency&gt;      
  </code></pre>
  
  <p>My code produced the following merged output xml file:</p>
  
  <pre><code> &lt;dependency&gt;
   &lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;ownedtestcase ID="UT001_GetPatientInfo" Package="emrservicedesigntest" source="EMRService"&gt;
  &lt;cutdependency ID="EMRService" Package="emrservicedesign"/&gt;
  &lt;/ownedtestcase&gt;
  
  &lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;ownedtestcase ID="UT001_ScheduleTreatment" Package="emrservicedesigntest" source="EMRService"&gt;
  &lt;cutdependency ID="Symptom" Package="emrservicedesign"/&gt;
  &lt;cutdependency ID="PatientInfo" Package="emrservicedesign"/&gt;
  &lt;/ownedtestcase&gt;
  &lt;/dependency&gt;` 
  </code></pre>
  
  <p>This is my code in java: </p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.FileInputStream;
  import java.io.FileWriter;
  import java.io.InputStreamReader;
  import java.io.Writer;
  
  public class Class {
  
  public static void main(String[] args) throws Exception {
  
  // Creates file to write to
  Writer output = null;
  output = new BufferedWriter(new FileWriter("merged_xml.xml"));
  String newline = System.getProperty("line.separator");
  output.write("&lt;dependency&gt;");
  
  // Read in xml file 1
  FileInputStream in = new FileInputStream("C:/Users/modifiedscheduletreatment.xml");
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  
  while ((strLine = br.readLine()) != null) {
  
  if (strLine.contains("&lt;dependency&gt;")){
  strLine = strLine.replace("&lt;dependency&gt;", "");
  }
  if (strLine.contains("&lt;/dependency&gt;")){
  strLine = strLine.replace("&lt;/dependency&gt;", "");
  }
  
  output.write(newline);
  output.write(strLine);
  
  //System.out.println(strLine);
  }
  
  // Read in xml file 2
  FileInputStream in2 = new FileInputStream("C:/Users/modifiedgetpatientinfo.xml");
  BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
  String strLine2;
  
  while ((strLine2 = br2.readLine()) != null) {
  
  if (strLine2.contains("&lt;dependency&gt;")){
  strLine2 = strLine2.replace("&lt;dependency&gt;", "");
  }
  if (strLine2.contains("&lt;/dependency&gt;")){
  strLine2 = strLine2.replace("&lt;/dependency&gt;", "");
  }
  
  output.write(strLine2);
  output.write(newline);
  //System.out.println(strLine2);
  }
  
  output.write("&lt;/dependency&gt;");
  output.close();
  
  System.out.println("Merge Complete");
  
  }
  
  }
  </code></pre>
  
  <p>You can notice that I have two header in the output file unfortunately, since I have two xml file, and the root  becomes before header, I don't know what should I modify to fix these two problems in the merged output xml file. Thanks in Advance</p>
  
  <p>The expected output file must look like </p>
  
  <pre><code> &lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
   &lt;dependency&gt;
    &lt;ownedtestcase ID="UT001_GetPatientInfo" Package="emrservicedesigntest" source="EMRService"&gt;
  &lt;cutdependency ID="EMRService" Package="emrservicedesign"/&gt;
  &lt;/ownedtestcase&gt;
  
  &lt;ownedtestcase ID="UT001_ScheduleTreatment" Package="emrservicedesigntest" source="EMRService"&gt;
  &lt;cutdependency ID="Symptom" Package="emrservicedesign"/&gt;
  &lt;cutdependency ID="PatientInfo" Package="emrservicedesign"/&gt;
  &lt;/ownedtestcase&gt;
  &lt;/dependency&gt;
  </code></pre>