Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

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

__________________
ME
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-27-2008, 06:04 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 223
DonCash will become famous soon enoughDonCash will become famous soon enough
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-28-2008, 02:33 AM
Member
 
Join Date: Nov 2007
Posts: 3
veera is on a distinguished road
Thank you
Hi Dons,

Thank you verymuch. It works perfectly.

Veera

Quote:
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, 12:40 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 223
DonCash will become famous soon enoughDonCash will become famous soon enough
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
Sponsored Links
Reply


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

vB 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 08:43 PM
How do merge two xml files into one xml? veera XML 1 03-27-2008 06:06 PM
Merge 2 button become one banie AWT / Swing 1 02-17-2008 06:26 PM
Merge Sort Help Hollywood New To Java 5 01-30-2008 04:26 AM
PDF Split and Merge 0.7 beta 1 JavaBean Java Announcements 0 06-24-2007 09:46 AM


All times are GMT +3. The time now is 10:45 PM.


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