Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-27-2007, 01:23 PM
Member
 
Join Date: Nov 2007
Posts: 4
Rep Power: 0
alwz_nikhil is on a distinguished road
Default 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 !!

__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
ME
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 03-27-2008, 05:04 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 241
Rep Power: 3
DonCash will become famous soon enoughDonCash will become famous soon enough
Default
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 05:10 PM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-28-2008, 01:33 AM
Member
 
Join Date: Nov 2007
Posts: 4
Rep Power: 0
veera is on a distinguished road
Default Thank you
Hi Dons,

Thank you verymuch. It works perfectly.

Veera

Originally Posted by DonCash View Post
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-28-2008, 11:40 AM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 241
Rep Power: 3
DonCash will become famous soon enoughDonCash will become famous soon enough
Default
Brilliant! Glad I could help. If this issue has been resolved then please mark the thread with a tick to let others know.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 09-29-2008, 05:03 PM
Member
 
Join Date: Sep 2008
Posts: 5
Rep Power: 0
Huoliuhi is on a distinguished road
Arrow friend
Thanks! Bump!
__________________
hi
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Merge Sort in Java Java Tip Algorithms 0 04-15-2008 07:43 PM
How do merge two xml files into one xml? veera XML 1 03-27-2008 05:06 PM
Merge 2 button become one banie AWT / Swing 1 02-17-2008 05:26 PM
Merge Sort Help Hollywood New To Java 5 01-30-2008 03:26 AM
PDF Split and Merge 0.7 beta 1 JavaBean Java Announcements 0 06-24-2007 08:46 AM


All times are GMT +2. The time now is 04:23 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org