Reputation: 83
Suppose a class Output.java
contains 6 varibles, out of which 4 variables data is stored on database_A
and 2 variable data is stored in database_B
.
My class Delegate.java
need to communicate with two diffrent service Service_A.java
and Service_B.java
which will ask database_A
and database_B
respectively to fetch respective data (database_A
- 4 varibales, database_B
- 2 variables).
The communication between Delegate.java
and Service_A.java
/Service_B.java
is in form of XML request/response.
So Deleage.java
will have two XML as response, one from database_A
(called by Service_A.java
) and one from database_b
(called by Service_B.java
).
I want to merge these two XML files and make a singe XML file Final_xml
, which contains valuse of all 6 variables of Output.java
class.
Upvotes: 2
Views: 46307
Reputation: 1
You can consider XML as text file and combine them. That is very fast as compared to other methods. Please have a look at below code :-
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class XmlComb {
static Set<String> lstheader = new HashSet<String>();
public static void main(String[] args) throws IOException {
Map<String,List<String>> map1 = getMapXml("J:\\Users\\Documents\\XMLCombiner01\\src\\main\\resources\\File1.xml");
Map<String,List<String>> map2 = getMapXml("J:\\Users\\Documents\\XMLCombiner01\\src\\main\\resources\\File2.xml");
Map<String,List<String>> mapCombined = combineXML(map1, map2);
lstheader.forEach( lst -> {
System.out.println(lst);
});
try {
mapCombined.forEach((k,v) -> {
System.out.println(k);
v.forEach(val -> System.out.println(val));
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Map<String,List<String>> combineXML(Map<String, List<String>> map1, Map<String, List<String>> map2 ) {
Map<String,List<String>> map2Modified = new TreeMap<String, List<String>>();
Map<String,List<String>> mapCombined = new TreeMap<String, List<String>>();
// --- Modifying map ---
for(String strKey2 : map2.keySet()) {
if(map1.containsKey(strKey2)) {
map2Modified.put(strKey2.split("\">")[0] + "_1\">", map2.get(strKey2));
}
else {
map2Modified.put(strKey2 , map2.get(strKey2));
}
}
//---- Combining map ---
map1.putAll(map2Modified);
return map1;
}
public static Map<String,List<String>> getMapXml(String strFilePath) throws IOException{
File file = new File(strFilePath);
BufferedReader br = new BufferedReader(new FileReader(file));
Map<String, List<String>> testMap = new TreeMap<String, List<String>>();
List<String> lst = null;
String st;
String strCatalogName = null;
while ((st = br.readLine()) != null) {
//System.out.println(st);
if(st.toString().contains("<TestCase")){
lst = new ArrayList<String>();
strCatalogName = st;
testMap.put(strCatalogName, lst);
}
else if(st.contains("</TestCase")){
lst.add(st);
testMap.put(strCatalogName,lst);
}
else {
if(lst != null){
lst.add(st);
}else {
lstheader.add(st);
}
}
}
return testMap;
}
}
Upvotes: -1
Reputation: 3794
Yes its possible to merge xml files. You can refer below links to merge your file. Do necessary changes in code of below links to achieve structure of your XML. If possible share structure of your XML , will help you with the relevant code.
Merging xml file using java NodeList
Upvotes: 3