
11-27-2007, 02:23 PM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 4
Rep Power: 0
|
|
Merge Two Xml files ????
I have two xml files, which I want to merge.
Both are having same structure…
|
Code:
|
Eg. INPUT-FILE-1
<library>
<book isbn="1">
<name>Book-11</name>
<author>steve</author>
<price>2.99</price>
</book>
<book isbn="2">
<name>Book-22</name>
<author>john</author>
<price>12.99</price>
</book>
</library>
------------------------------------------------
INPUT-FILE-2
<library>
<book isbn="1">
<name>Book-11</name>
<author>steve</author>
<price>2.99</price>
</book>
<book isbn="3">
<name>Book-33</name>
<author>johnthan</author>
<price>12.99</price>
</book>
</library>
-------------------------------------------
OUTPUT-file
<library>
<book isbn="1">
<name>Book-11</name>
<author>steve</author>
<price>2.99</price>
</book>
<book isbn="2">
<name>Book-22</name>
<author>john</author>
<price>12.99</price>
</book>
</book>
<book isbn="3">
<name>Book-33</name>
<author>johnthan</author>
<price>12.99</price>
</book>
</library> |
Merge should provide third file without any repetition of any record.
Which parser to use and how to use ???
Any help on this ???????????
Thanks !!
|
|

03-27-2008, 06:04 PM
|
 |
Moderator
|
|
Join Date: Aug 2007
Location: London, UK
Posts: 241
Rep Power: 4
|
|
Hey.
I'm sure there are other ways of doing this but this is how I would do it.
|
Code:
|
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 Class1 {
public static void main(String[] args) throws Exception {
// Creates file to write to
Writer output = null;
output = new BufferedWriter(new FileWriter("merged_xml.txt"));
String newline = System.getProperty("line.separator");
output.write("<library>");
// Read in xml file 1
FileInputStream in = new FileInputStream("file_path/file1.xml");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
if (strLine.contains("<library>")){
strLine = strLine.replace("<library>", "");
}
if (strLine.contains("</library>")){
strLine = strLine.replace("</library>", "");
}
output.write(newline);
output.write(strLine);
//System.out.println(strLine);
}
// Read in xml file 2
FileInputStream in2 = new FileInputStream("file_path/file2.xml");
BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
String strLine2;
while ((strLine2 = br2.readLine()) != null) {
if (strLine2.contains("<library>")){
strLine2 = strLine2.replace("<library>", "");
}
if (strLine2.contains("</library>")){
strLine2 = strLine2.replace("</library>", "");
}
output.write(strLine2);
output.write(newline);
//System.out.println(strLine2);
}
output.write("</library>");
output.close();
System.out.println("Merge Complete");
}
} |
I have just tested this myself and it works perfectly. The output is exactly as you requested.
For this to be outputted as .xml you will need to change the output file from .txt to .xml. Obviousally, for this to load correctly, you will need to make sure the tags are formatted properly.
If you find this useful then please +rep me!
Thanks
Last edited by DonCash; 03-27-2008 at 06:10 PM.
|
|

03-28-2008, 02:33 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 4
Rep Power: 0
|
|
Thank you
Hi Dons,
Thank you verymuch. It works perfectly.
Veera
Originally Posted by DonCash
|
Hey.
I'm sure there are other ways of doing this but this is how I would do it.
|
Code:
|
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 Class1 {
public static void main(String[] args) throws Exception {
// Creates file to write to
Writer output = null;
output = new BufferedWriter(new FileWriter("merged_xml.txt"));
String newline = System.getProperty("line.separator");
output.write("<library>");
// Read in xml file 1
FileInputStream in = new FileInputStream("file_path/file1.xml");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
if (strLine.contains("<library>")){
strLine = strLine.replace("<library>", "");
}
if (strLine.contains("</library>")){
strLine = strLine.replace("</library>", "");
}
output.write(newline);
output.write(strLine);
//System.out.println(strLine);
}
// Read in xml file 2
FileInputStream in2 = new FileInputStream("file_path/file2.xml");
BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
String strLine2;
while ((strLine2 = br2.readLine()) != null) {
if (strLine2.contains("<library>")){
strLine2 = strLine2.replace("<library>", "");
}
if (strLine2.contains("</library>")){
strLine2 = strLine2.replace("</library>", "");
}
output.write(strLine2);
output.write(newline);
//System.out.println(strLine2);
}
output.write("</library>");
output.close();
System.out.println("Merge Complete");
}
} |
I have just tested this myself and it works perfectly. The output is exactly as you requested.
For this to be outputted as .xml you will need to change the output file from .txt to .xml. Obviousally, for this to load correctly, you will need to make sure the tags are formatted properly.
If you find this useful then please +rep me!
Thanks
|
|
|

03-28-2008, 12:40 PM
|
 |
Moderator
|
|
Join Date: Aug 2007
Location: London, UK
Posts: 241
Rep Power: 4
|
|
|
Brilliant! Glad I could help. If this issue has been resolved then please mark the thread with a tick to let others know.
|
|

12-30-2009, 03:59 PM
|
|
Member
|
|
Join Date: Dec 2009
Posts: 1
Rep Power: 0
|
|
Hi, I needed something similar and this is how I've done it, basically I need to to insert xml to another xml - xml document inside <Body></Body> tags of xml evelope.
Maybe it's not the same thing you need, but maybe you'll find something you could use in your case.
|
Quote:
|
/**
* Returns xml representation of the request - envelope with the request.
*
* @param envelope xml string envelope used for sending this request
* @param body xml string body of the envelope - the actual request
* @return xml string of the envelope and the request
*/
private String getXml(String envelope, String body) {
/* envelope document */
Document envelopeDocument = null;
/* insert body to the envelope */
try {
DocumentBuilderFactory dbfac= DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbfac.newDocumentBuilder();
/* load envelope */
envelopeDocument = builder.parse(new ByteArrayInputStream(envelope.getBytes()));
/* find body element in the envelope */
NodeList nodeList = envelopeDocument.getElementsByTagName("Body");
Node bodyNode = nodeList.item(0);
/* insert body inside */
if (bodyNode != null) {
/* get root element of the body */
Document bodyDocument = builder.parse(new ByteArrayInputStream(body.getBytes()));
Element bodyRootElement = bodyDocument.getDocumentElement();
/* node has to be tied to the same document, so we need to do this */
Node nd = envelopeDocument.importNode(bodyRootElement, true);
bodyNode.appendChild(nd);
}
} catch (SAXException ex) {
Logger.getLogger(SimpleEnvelope.class.getName()).l og(Level.SEVERE, null, ex);
} catch (ParserConfigurationException ex) {
Logger.getLogger(SimpleEnvelope.class.getName()).l og(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(SimpleEnvelope.class.getName()).l og(Level.SEVERE, null, ex);
}
/* string request to be sent */
String request = null;
/* convert to string */
try {
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.INDENT, "yes");
/* create string from xml tree */
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(envelopeDocument);
trans.transform(source, result);
request = sw.toString();
} catch (TransformerConfigurationException ex) {
Logger.getLogger(SimpleEnvelope.class.getName()).l og(Level.SEVERE, null, ex);
} catch (TransformerException ex) {
Logger.getLogger(SimpleEnvelope.class.getName()).l og(Level.SEVERE, null, ex);
}
return request;
}
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 07:27 AM.
|
|
VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org