2

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.

Mayank Tiwari
  • 83
  • 1
  • 2
  • 11
  • See also: https://stackoverflow.com/questions/9377949/combining-multiple-xml-documents-into-one-large-one-with-a-batch-file/51497240#51497240 – Rusi Popov Jul 24 '18 at 11:25

2 Answers2

3

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.

Merge Two XML Files in Java

Merging xml file using java NodeList

Community
  • 1
  • 1
Alpesh Gediya
  • 3,706
  • 1
  • 25
  • 38
  • Worried about formatting XML ---------------------------- Thanks for response. My goal is to achieve Existing xml format of Current_xml by merging two new xml .It is sure that when I will merge these two new xml file new1_xml and new2_xml I will get all data which is which is required for Existing_xml file. But providing the same format, which is Current_xml is seems difficult for me. – Mayank Tiwari May 20 '13 at 18:35
  • Okay, I hope you will solve your problem of formatting by looking at examples in link. – Alpesh Gediya May 21 '13 at 02:51
  • 1
    Thanks Alpesh, I dont want hardcoded merging concept. I want to write some java code, which will identify Nodes and child of xml , then parse uncommon data and put it in new xml file. After that I will merge it with one of existing xml file. I am able to parse xml using DOM and still struggling for desired solution. – Mayank Tiwari May 28 '13 at 18:10
-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;
    }
}