简体   繁体   中英

Merging two xml files in one file

I have written code in Java to merge two xml file, the first file is:

<?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>`  

The second file is:

<?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>      

My code produced the following merged output xml file:

 <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>` 

This is my code in 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");

}

}

You can notice that I have two header in the output file unfortunately, since I have two xml file, and the root becomes before header, I don't know what should I modify to fix these two problems in the merged output xml file. Thanks in Advance

The expected output file must look like

 <?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>

It seems you are just missing removing the XML header tags. You need to add another filter, similar to how you took out the dependency tags, for the XML header tags.

just skip the first two lines and the last, count the number of lines you are going to read and then use that count:

//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);
        }

You might be better served navigating the DOM with a tool like JDom (there are a number of options.) Even better if you can work in XPath, which will let you lift the nodes out of the target docs and add them to a new doc.

Something like this, not complete code, but should get you started:

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

There are other ways to work with the dom, but this could get you started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM