Merge xml files in java

Merging two XML files into one XML file using Java

I am stuck with how to proceed with combining two different XML files(which has the same structure). When I was doing some research on it, people say that XML parsers like DOM or StAX will have to be used. But cant I do it with the regular IOStream? I am currently trying to do with the help of IOStream but this is not solving my purpose, its being more complex. For example, What I have tried is;

 public class GUI < public static void main(String[] args) throws Exception < // Creates file to write to Writer output = null; output = new BufferedWriter(new FileWriter("C:\\merged.xml")); String newline = System.getProperty("line.separator"); output.write(""); // Read in xml file 1 FileInputStream in = new FileInputStream("C:\\1.xml"); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; while ((strLine = br.readLine()) != null) < if (strLine.contains(""))< strLine = strLine.replace("", "xmlns:xsi"); > if (strLine.contains(""))< strLine = strLine.replace("", "xmlns:xsd"); > output.write(newline); output.write(strLine); System.out.println(strLine); > // Read in xml file 2 FileInputStream in = new FileInputStream("C:\\2.xml"); BufferedReader br1 = new BufferedReader(new InputStreamReader(in)); String strLine1; while ((strLine1 = br1.readLine()) != null) < if (strLine1.contains(""))< strLine1 = strLine1.replace("", ""); > if (strLine1.contains(""))< strLine1 = strLine1.replace("", ""); > output.write(newline); output.write(strLine1); 

I request you to kindly let me know how do I proceed with merging two XML files by adding additional content as well. It would be great if you could provide me some example links as well. Thank You in Advance. System.out.println(strLine1); > >

3 Answers 3

Not exactly sure what you’re looking to do. By merging do you mean:

a. You want to merge the content for the 2 DOMs and come up with one object model (a valid one) with appended nodes

b. You want to merge the 2 files one after the other and not care about the actual content

If it’s a, use XML parsers. Sure you can write the thing by hand and try to process the streams into dom objects but you’ll be rewriting a lot of what those parsers are for. Why rewrite something that’s already there.

If it’s b, just do a dumb copy. Copy the first file (use utilities again, stuff like apache common’s FileUtil allows you to copy files. Don’t write unless necessary), open an IO stream to the copied file then read and write the second file over.

I would like to merge two XML files and write it to another XML file. Sorry for not being clear earlier.

If you’re merging the contents of the files, read both (using parsers like JDOM) into dom objects, create a brand new object that you will append to, traverse the model of the original 2 objects, append to new object. Rinse and repeat

I am not sure if I have understood your point correctly. I will have to read ‘file1’ using JDOM parser and take the necessary contents from it to add it to ‘file2’, is it.?

He means: 1. read each object using the parser and create DOM objects for both of them. 2. create a brand new object with the same structure. 3. traverse the xml model of the new object. While you do this read the original two dom objects’ values and append them into your new DOM object. 4. profit. In the end you will have three DOM objects. Original xml object #1, Original xml object #2, and your new combined xml object 3. Answer upvoted.

Читайте также:  Sqlite3 python вывод данных

As a general rule, never do any processing of XML at the lexical level: always use an XML parser. (You can break this rule if (a) you are an XML expert, so you know what might go wrong, and (b) you know that the results don’t have to be correct all the time.)

Secondly, the easiest way to do this kind of processing is to use a language designed for the job, like XSLT or XQuery. Using Java makes it very lard work.

If you’re more specific about the kind of files you want merged and what you want the output to look like, we can give you a more precise answer.

Yes I can extend my doubt. I want to merge two ‘XML’ files and the output should also be an ‘XML’ file. In the first file, there are two nodeAttributes which are missing in the second file and in the second file, there is another tag . which is missing in the first file.

I am trying to using XSLT currently, but this makes my job very complex. Can I know what is File xslt = new File(» «), I mean what is file to be given as an xslt file, in my case? Coz I currently have just two xml files.

Both XSLT and DOM operate at the level of a tree of nodes, not at the level of start and end tags. You need to think about the tree transformation to be performed, not about tags to be inserted into the lexical XML

package com.cts.sterling.order; //package com.academy.ecommerce.sterling.userexits; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import com.cts.sterling.custom.accelerators.util.XMLUtil; public class MergeXml < public static Document mergeXml(Document doc1, Document doc2) throws Exception < // Getting the attributes of OrderLine from document2 and document1 NodeList objOrderLineList2 = doc2.getElementsByTagName("OrderLine"); NodeList objOrderLineList1 = doc1.getElementsByTagName("OrderLine"); // Creating Element for OrderLine Element eleOrderline2 = (Element) objOrderLineList2.item(0); Element eleOrderline1 = (Element) objOrderLineList1.item(0); // Declaring attributes as String array String[] Line1=; // Copying attributes from document2 to document1 XMLUtil.copyAttributes(eleOrderline2, eleOrderline1, Line1); // Getting the attributes of Extn from document2 and document1 NodeList objExtn2 = doc2.getElementsByTagName("Extn"); NodeList objExtn1 =doc1.getElementsByTagName("Extn"); // Creating Element for Extn Element eleExtn2 = (Element) objExtn2.item(0); Element eleExtn1 = (Element) objExtn1.item(0); // Declaring attributes as String array String[] Line2=; // Copying attributes from document2 to document1 XMLUtil.copyAttributes(eleExtn2, eleExtn1, Line2); // Getting the attributes of WSIAddnlOrderLineData from document2 and document1 NodeList objAddln2 = doc2.getElementsByTagName("WSIAddnlOrderLineData"); NodeList objAddln1 =doc1.getElementsByTagName("WSIAddnlOrderLineData"); // Creating Element for WSIAddnlOrderLineData Element eleAddln2 = (Element) objAddln2.item(0); Element eleAddln1 = (Element) objAddln1.item(0); // Declaring attributes as String array String[] Line3 =; // Copying attributes from document2 to document1 XMLUtil.copyAttributes(eleAddln2, eleAddln1, Line3); // Getting the attributes of Instruction from document2 and document1 NodeList objInst2 = doc2.getElementsByTagName("Instruction"); NodeList objInst1 =doc1.getElementsByTagName("Instruction"); // Creating Element for Instruction Element eleInst2 = (Element) objInst2.item(0); Element eleInst1 = (Element) objInst1.item(0); // Declaring attributes as String array String[] Line4 =; // Copying attributes from document2 to document1 XMLUtil.copyAttributes(eleInst2, eleInst1, Line4); //Printing output document System.out.println(XMLUtil.getString(doc1)); return doc1; > //Main method public static void main(String[] args) < try< File file1 = new File("D:/Handson/merge1.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc1 = dBuilder.parse(file1); File file2 = new File("D:/Handson/merge2.xml"); Document doc2 = dBuilder.parse(file2); //calling the method mergeXml(doc1,doc2); >catch (Exception e) < e.printStackTrace(); >> > 

Where can we find that XMLUtil class? Without out this answer isn’t much help. (as pointed out by user Mara4you)

Читайте также:  Php заполнить массив числами от 0 до 100

Источник

Merge two XML files in java and make single xml

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.

2 Answers 2

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.

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.

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.

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 Setlstheader = new HashSet(); public static void main(String[] args) throws IOException < Map map1 = getMapXml("J:\\Users\\Documents\\XMLCombiner01\\src\\main\\resources\\File1.xml"); Map map2 = getMapXml("J:\\Users\\Documents\\XMLCombiner01\\src\\main\\resources\\File2.xml"); Map 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 combineXML(Map map1, Map map2 ) < Map map2Modified = new TreeMap(); Map mapCombined = new TreeMap(); // --- 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 getMapXml(String strFilePath) throws IOException< File file = new File(strFilePath); BufferedReader br = new BufferedReader(new FileReader(file)); Map testMap = new TreeMap(); List lst = null; String st; String strCatalogName = null; while ((st = br.readLine()) != null) < //System.out.println(st); if(st.toString().contains(" else if(st.contains(" else < if(lst != null)< lst.add(st); >else < lstheader.add(st); >> > return testMap; > > 

Источник

Merge XML documents [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

I need to «merge» two XML documents, overwriting the overlapsed attributes and elements. For instance if I have document1:

I prefer Java or XSLT-based solutions, ant will do fine, but if there’s an easy way to do that in Rake, Ruby or Python please don’t be shy 🙂 EDIT: actually I find I’d rather use an automated tool/script, even writing it by myself, because manually merging some 30 XML files is a bit unwieldy. 🙁

Can you be a little more specific about the rules you want to apply? Will there always only be one element with the same name? Do you go deeper into the tree to compare child elements when both documents have the same element (with identical attributes)?

5 Answers 5

If you like XSLT, there’s a nice merge script I’ve used before at: Oliver’s XSLT page

I know this is an old thread, but Project: Merge can do this for you. It can merge two XML files together, and can be run from the command line, so you can batch everything up together, run it and just resolve any conflicts (such as the changing attribute value of ‘key’ in your above example) manually with a few clicks. (You can tell it to run silently providing there are no conflicts.)

It can perform two-way and three-way comparisons of XML files and two-way and three-way merges. (Where a three-way operation assumes the two files being compared/merged have a common ancestor.)

According to their webpage Project: Merge is the predecessor to Oso XML Merge, and can therefore be considered as a discontinued product at this time.

Oso XML Merge has improved functionality over Project: Merge. More information is available at the Oso Corporation website here: osocorporation.com/xmlmerge/index.php (Disclaimer: Oso Corporation is my company, Project: Merge/Oso XML Merge are my products.) 🙂

Check XmlCombiner which is a Java library that implements XML merging in exactly this way. It is loosely based on a similar functionality offered by plexus-utils library.

XmlCombiner default convention is to overwrite the overlapping attributes and elements. But the exact merging behavior can be altered using special ‘combine.self’ and ‘combine.children’ attributes.

import org.atteo.xmlcombiner.XmlCombiner; // create combiner XmlCombiner combiner = new XmlCombiner(); // combine files combiner.combine(firstFile); combiner.combine(secondFile); // store the result combiner.buildDocument(resultFile); 

Disclaimer: I am the author.

XSLT merge from PowerShell:

param( [Parameter(Mandatory = $True)][string]$file1, [Parameter(Mandatory = $True)][string]$file2, [Parameter(Mandatory = $True)][string]$path ) # using only abs paths .. just to be safe $file1 = Join-Path $(Get-Location) $file1 $file2 = Join-Path $(Get-Location) $file2 $path = Join-Path $(Get-Location) $path # awesome xsl stylesheet from Oliver Becker # http://web.archive.org/web/20160502194427/http://www2.informatik.hu-berlin.de/~obecker/XSLT/merge/merge.xslt $xsltfile = Join-Path $(Get-Location) "merge.xslt" $XsltSettings = New-Object System.Xml.Xsl.XsltSettings $XsltSettings.EnableDocumentFunction = 1 $xslt = New-Object System.Xml.Xsl.XslCompiledTransform; $xslt.Load($xsltfile , $XsltSettings, $(New-Object System.Xml.XmlUrlResolver)) [System.Xml.Xsl.XsltArgumentList]$al = [System.Xml.Xsl.XsltArgumentList]::new() $al.AddParam("with", "", $file2) $al.AddParam("replace", "", "true") [System.Xml.XmlWriter]$xmlwriter = [System.Xml.XmlWriter]::Create($path) $xslt.Transform($file1, $al, $xmlwriter) 
java -jar saxon9he.jar .\FileA.xml .\merge.xslt with=FileB.xml replace=true 

Источник

Оцените статью