Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6813921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:37:36+00:00 2026-05-26T20:37:36+00:00

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

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:37:36+00:00Added an answer on May 26, 2026 at 8:37 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a Java code which imports an external jar file. How can
I have written code in Java to access web cam,and to save image... I
In our application, I have seen code written like this: User.java (User entity) public
I have written jQuery code, in files Main.html and ajax.php . The ajax.php file
I am trying to use custom font in android. I have written java code
I have written the java code below, which executes another java program named Newsworthy_RB.
I have written a program that reads in a File object (really an XML
We have written a bunch of Java code and need to package this code
I have written some code which passes values between java and c using jni.
I have written a java code in Eclipse and i am developing a plug-in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.