Originally Posted by
DonCash
Hey.
I'm sure there are other ways of doing this but this is how I would do it.
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 Class1 {
public static void main(String[] args) throws Exception {
// Creates file to write to
Writer output = null;
output = new BufferedWriter(new FileWriter("merged_xml.txt"));
String newline = System.getProperty("line.separator");
output.write("<library>");
// Read in xml file 1
FileInputStream in = new FileInputStream("file_path/file1.xml");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
if (strLine.contains("<library>")){
strLine = strLine.replace("<library>", "");
}
if (strLine.contains("</library>")){
strLine = strLine.replace("</library>", "");
}
output.write(newline);
output.write(strLine);
//System.out.println(strLine);
}
// Read in xml file 2
FileInputStream in2 = new FileInputStream("file_path/file2.xml");
BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
String strLine2;
while ((strLine2 = br2.readLine()) != null) {
if (strLine2.contains("<library>")){
strLine2 = strLine2.replace("<library>", "");
}
if (strLine2.contains("</library>")){
strLine2 = strLine2.replace("</library>", "");
}
output.write(strLine2);
output.write(newline);
//System.out.println(strLine2);
}
output.write("</library>");
output.close();
System.out.println("Merge Complete");
}
}
I have just tested this myself and it works perfectly. The output is exactly as you requested.
For this to be outputted as .xml you will need to change the output file from .txt to .xml. Obviousally, for this to load correctly, you will need to make sure the tags are formatted properly.
If you find this useful then please +rep me!
Thanks
