Tuesday, 28 May 2013

How to merge two XML files have the same parameters?

How to merge two XML files have the same parameters?

I want to merge two XML files(source file & temp file) and put the resulted file in the source file and both source and temp files have the same elements but with different values like :
Source.xml:
<Main>
   <source>
        <param>
            <entry>
                <key> bla1 </key>
                <value> bla1 </value>
            </entry>
        </param>
        <Name> name1 </Name>
   </Source>
</Main>
And temp.xml:
<Main>
   <source>
        <param>
           <entry>
               <key> bla2 </key>
               <value> bla2 </value>
           </entry>
           <entry>
               <key> bla3 </key>
               <value> bla3 </value>
           </entry> 
        </param>
        <Name> name2 </Name>
   </Source>
</Main>
And the desired output i want it like :
<Main>
  <source>
        <param>
            <entry>
                <key> bla1 </key>
                <value> bla1 </value>
            </entry>
        </param>
        <Name> name1 </Name>
   </Source>
   <source>
        <param>

           <entry>
               <key> bla2 </key>
               <value> bla2 </value>
           </entry>
           <entry>
               <key> bla3 </key>
               <value> bla3 </value>
           </entry> 
        </param>
        <Name> name2 </Name>
   </Source>
</Main>
I'm using this code but it does't affect the source.xml at all :
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class MergeXml {

    private st

No comments:

Post a Comment