Results 1 to 6 of 6
  1. #1
    alwz_nikhil is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Default Merge Two Xml files ????

    I have two xml files, which I want to merge.
    Both are having same structure…


    Java Code:
    [B]Eg. INPUT-FILE-1[/B]
    <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>
    ------------------------------------------------
    [B]INPUT-FILE-2[/B]
    <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>
    -------------------------------------------
    [B]OUTPUT-file[/B]
    <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 !!


  2. #2
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    Hey.

    I'm sure there are other ways of doing this but this is how I would do it.

    Java 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.

  3. #3
    veera is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Default 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.

    Java 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 :)

  4. #4
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    Brilliant! Glad I could help. If this issue has been resolved then please mark the thread with a tick to let others know.

  5. #5
    pete911 is offline Member
    Join Date
    Dec 2009
    Posts
    1
    Rep Power
    0

    Default

    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.

    /**
    * 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;
    }

  6. #6
    virginie is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default Merger more than two XML files

    Hi,

    I found your solution quite chic and I tried it for many XML files. The result ist not what I have expected. I have something like this:

    <body>
    <line1.1> </line1.1>
    <line1.2></line1.2>
    <body>
    <line2.1> </line2.1>
    <line2.2></line2.2>
    </body>
    <body>
    <line3.1> </line3.1>
    <line3.2></line3.2>
    </body>
    </body>

    Hier is the code I transformed:
    private void stelleDateienWiederZusammen(ArrayList<File> files, ClassInfo classInfo, File file) {

    /* envelope document */
    Document envelopeDocument = null;

    /* insert body to the envelope */
    try {
    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = dbfac.newDocumentBuilder();

    /* load envelope */
    envelopeDocument = builder.parse(files.get(0));
    NodeList nodeList = envelopeDocument.getElementsByTagName(classInfo.ge tShortObjectName());
    Node bodyNode = nodeList.item(0);

    if (bodyNode != null) {
    for (int i = 1; i < files.size(); i++) {
    Document bodyDocument = builder.parse(files.get(i));
    Element bodyRootElement = bodyDocument.getDocumentElement();
    Node nd = envelopeDocument.importNode(bodyRootElement, true);
    bodyNode.appendChild(nd);

    }
    for (int i = 1; i < files.size(); i++) {
    files.get(i).deleteOnExit();
    }

    }
    } catch (SAXException ex) {
    ex.printStackTrace();
    // Logger.getLogger(SimpleEnvelope.class.getName()).l og(Level.SEVERE,
    // null, ex);
    } catch (ParserConfigurationException ex) {
    ex.printStackTrace();
    // Logger.getLogger(SimpleEnvelope.class.getName()).l og(Level.SEVERE,
    // null, ex);
    } catch (IOException ex) {
    ex.printStackTrace();
    // Logger.getLogger(SimpleEnvelope.class.getName()).l og(Level.SEVERE,
    // null, ex);
    } catch (Exception ex) {
    ex.printStackTrace();
    // Logger.getLogger(SimpleEnvelope.class.getName()).l og(Level.SEVERE,
    // null, ex);
    }


    try {
    TransformerFactory transfac = TransformerFactory.newInstance();
    Transformer trans = transfac.newTransformer();
    trans.setOutputProperty(OutputKeys.INDENT, "yes");
    StreamResult result = new StreamResult(file);
    DOMSource source = new DOMSource(envelopeDocument);
    trans.transform(source, result);

    } catch (TransformerConfigurationException ex) {
    ex.printStackTrace();
    } catch (TransformerException ex) {
    ex.printStackTrace();

    }
    }

    What I need is a file like this:
    <body>
    <line1.1> </line1.1>
    <line1.2></line1.2>
    <line2.1> </line2.1>
    <line2.2></line2.2>
    <line3.1> </line3.1>
    <line3.2></line3.2>
    </body>

    Can you tell me please what is false in my Code?

    Thanks

Similar Threads

  1. Merge Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 08:43 PM
  2. Replies: 1
    Last Post: 03-27-2008, 06:06 PM
  3. Merge 2 button become one
    By banie in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2008, 06:26 PM
  4. Merge Sort Help
    By Hollywood in forum New To Java
    Replies: 5
    Last Post: 01-30-2008, 04:26 AM
  5. PDF Split and Merge 0.7 beta 1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 06-24-2007, 09:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •