在一个文件中合并两个xml文件

时间:2011-11-18 00:08:32

标签: java xml-serialization

我用Java编写代码来合并两个xml文件, 第一个文件是:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<dependency>
<ownedtestcase ID="UT001_GetPatientInfo" Package="emrservicedesigntest" source="EMRService">
<cutdependency ID="EMRService" Package="emrservicedesign"/>
</ownedtestcase>
</dependency>`  

第二个文件是:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<dependency>
<ownedtestcase ID="UT001_ScheduleTreatment" Package="emrservicedesigntest" source="EMRService">
<cutdependency ID="Symptom" Package="emrservicedesign"/>
<cutdependency ID="PatientInfo" Package="emrservicedesign"/>
</ownedtestcase>
</dependency>      

我的代码生成了以下合并输出xml文件:

 <dependency>
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ownedtestcase ID="UT001_GetPatientInfo" Package="emrservicedesigntest" source="EMRService">
<cutdependency ID="EMRService" Package="emrservicedesign"/>
</ownedtestcase>

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ownedtestcase ID="UT001_ScheduleTreatment" Package="emrservicedesigntest" source="EMRService">
<cutdependency ID="Symptom" Package="emrservicedesign"/>
<cutdependency ID="PatientInfo" Package="emrservicedesign"/>
</ownedtestcase>
</dependency>` 

这是我在java中的代码:

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("<dependency>");

// 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("<dependency>")){
strLine = strLine.replace("<dependency>", "");
}
if (strLine.contains("</dependency>")){
strLine = strLine.replace("</dependency>", "");
}

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("<dependency>")){
strLine2 = strLine2.replace("<dependency>", "");
}
if (strLine2.contains("</dependency>")){
strLine2 = strLine2.replace("</dependency>", "");
}

output.write(strLine2);
output.write(newline);
//System.out.println(strLine2);
}

output.write("</dependency>");
output.close();

System.out.println("Merge Complete");

}

}

你可能会注意到我在输出文件中有两个标题,因为我有两个xml文件,并且根目录变成了标题,我不知道应该修改什么来修复合并输出xml中的这两个问题文件。在此先感谢

预期的输出文件必须类似于

 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <dependency>
  <ownedtestcase ID="UT001_GetPatientInfo" Package="emrservicedesigntest" source="EMRService">
<cutdependency ID="EMRService" Package="emrservicedesign"/>
</ownedtestcase>

<ownedtestcase ID="UT001_ScheduleTreatment" Package="emrservicedesigntest" source="EMRService">
<cutdependency ID="Symptom" Package="emrservicedesign"/>
<cutdependency ID="PatientInfo" Package="emrservicedesign"/>
</ownedtestcase>
</dependency>

3 个答案:

答案 0 :(得分:1)

您似乎只是缺少删除XML标头标签。您需要为XML标头标记添加另一个过滤器,类似于您取出依赖关系标记的方式。

答案 1 :(得分:0)

只需跳过前两行和最后一行,计算您要读取的行数,然后使用该计数:

//count number of lines
    int counter = 0;
    while (br.readLine()) {
        counter++
    }
//use that count to not write first two, and last line
    for(int i; i < counter;i++){
        if (i <= 2 || i == counter){
            br.readLine();
        }else {
            output.write(br.readLine());
            output.write(newline);
        }

答案 2 :(得分:0)

使用像JDom这样的工具导航DOM可能会更好(有很多选项。)如果你可以在XPath中工作更好,这将让你将节点从目标文档中提取出来并添加到一个新的文档。

像这样的东西,不是完整的代码,但应该让你开始:

  InputStream inputStream= new FileInputStream("file.xml");
  Document request = new SAXBuilder().build(inputStream);

  final List elements = XPath.selectNodes(request, "//ownedtestcase");

  final Document newDoc = new Document(new Element("dependency"));
  final Element root = newDoc.getRootElement();

  for(Object elem : elements){
    root.addContent((Element)elem);
  }

  XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
  java.io.FileWriter writer = new java.io.FileWriter(fileName);
  out.output(newDoc, writer);
  writer.flush();
  writer.close();

还有其他方法可以使用dom,但这可以让你开始。