Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何组合这两个xml文件_Java - Fatal编程技术网

Java 如何组合这两个xml文件

Java 如何组合这两个xml文件,java,Java,文件employe=newfilee:five/emplo.xml; 文件stud=newfilee:/one/two/student.xml; 如何将这两个文件合并到一个文件对象中如果要合并两个标准文本文件,则可以使用文件写入程序和文件读取器 我假设这不是特定于xml的事情,因为我对它们没有经验 以下是如何在不进行异常处理的情况下读取文件: FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(

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

如果要合并两个标准文本文件,则可以使用文件写入程序和文件读取器

我假设这不是特定于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
}
您可以将每个文件读入字符串的arraylist,然后使用以下命令输出:

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

或者您也可以使用SAXParser

Hi visakh,欢迎使用SO。请向我们展示您尝试过的内容,我们可以尝试提供帮助。您想合并内容并重新打开它吗?请提供两个示例和所需的输出。您想合并两个XML文档,使其在一个根目录下正确格式,并简单地将一个文件附加到另一个文件吗?由于缺乏信息,被否决,导致其他人无法回答问题。如果问题已更新,我将取消。DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance;DocumentBuilder dBuilder=dbFactory.newDocumentBuilder;文件employe=newfilee:five/emplo.xml;文件stud=newfilee:/one/two/student.xml;文档doc1=dBuilder.parseemploye;文档doc2=dBuilder.parsestud;doc1.getDocumentElement.normalize;doc2.getDocumentElement.normalize;但是ididnt添加两个文件可以帮助MedDocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance;DocumentBuilder dBuilder=dbFactory.newDocumentBuilder;文件employe=newfilee:five/emplo.xml;文件stud=newfilee:/one/two/student.xml;文档doc1=dBuilder.parseemploye;文档doc2=dBuilder.parsestud;doc1.getDocumentElement.normalize;doc2.getDocumentElement.normalize;这是我使用dom parser编写的代码iam,我想将这两个文件合并到一个文件中。您可以使用DocumentBuilder合并这些文件然后进行分析吗?如果您可以合并这些文件然后进行分析,那么可以使用FileReader/FileWriter将这些文件合并到一个新文件中,然后可以分析这一个文件。
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/>