2012-11-12 80 views
-2

文件employe =新文件(“E:five/emplo.xml”); 文件stud =新文件(“E:/one/two/student.xml”); 如何将这两个文件合并到一个文件对象中如何合并这两个xml文件

+0

嗨visakh,欢迎来到SO。请告诉我们你已经尝试了什么,我们可以尝试提供帮助。 – Ben

+0

你想合并内容并重新打开它吗? –

+0

请提供两个例子和所需的输出。 –

回答

0

如果您想要合并两个标准文本文件,那么您可以使用文件写入器和文件读取器。

我假设这不是一些XML特定的事情,因为我没有经历过他们。

下面是如何读取文件(没有异常处理):

FileReader fr = new FileReader(file); 
BufferedReader br = new BufferedReader(fr); // the only reason I use this is because I am used to line by line handling 
String line; 
while((line = br.readLine()) != null) 
{ 
    // do something with each line 
} 

你可以读取每个文件到字符串数组列表,然后输出使用:

FileWriter fout = new FileWriter(file, toAppend); 
fout.write(msg); 
fout.close(); 
+0

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); \t \t \t DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); \t \t \t File employe = new File(“E:five/emplo.xml”); \t \t \t文件stud = new File(“E:/one/two/student.xml”); \t \t \t \t \t Document doc1 = dBuilder.parse(employe); \t \t \t Document doc2 = dBuilder.parse(stud); \t \t \t doc1.getDocumentElement()。normalize(); \t \t doc2.getDocumentElement()。normalize(); –

+0

这是我使用dom解析器的iam代码,我希望将这两个文件合并到单个文件中 –

+0

请任何人都可以帮我 –

0
String[] filenames = new String[]{ "emplo.xml", "student.xml"}; 
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream("merged.xml"); 
for (String filename : filenames) { 
    InputStream inputStream = new BufferedInputStream(new FileInputStream(filename); 
    org.apache.commons.io.IOUtils.copy(inputStream, outputStream); 
    inputStream.close(); 
} 
outputStream.close();<br/> 

或你也可以用SAXParser

+0

iam使用dom解析器 –

+0

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); \t \t \t DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); \t \t \t File employe = new File(“E:five/emplo.xml”); \t \t \t文件stud = new File(“E:/one/two/student.xml”); \t \t \t \t \t Document doc1 = dBuilder。解析(雇工); \t \t \t Document doc2 = dBuilder.parse(stud); \t \t \t doc1.getDocumentElement()。normalize(); \t \t doc2.getDocumentElement()。normalize(); –

+0

但ididnt添加两个文件可以帮助我 –