gpt4 book ai didi

java - 将两个 xml 文件合并到一个文件中

转载 作者:行者123 更新时间:2023-12-01 15:40:13 25 4
gpt4 key购买 nike

我用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");

}

}

不幸的是,您可以注意到输出文件中有两个 header ,因为我有两个 xml 文件,并且根变为 header 之前,我不知道应该修改什么来解决合并输出 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>

最佳答案

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

关于java - 将两个 xml 文件合并到一个文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8176176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com