Hello everyone. I new here and in Java programming as well. As the title says, I want to merge some XML files which are in a list, I don't know how to do this. I explain further what I want to do with the example below
First of all, I have the XML files in a arraylist created in the java program before, so it has thes form
ArrayList <Files> folder=new ArrayList <Files>
.
2) My XML files all have this format
<nta>
             <declaration> a1 </declaration>
             <template_a1> abc </template_a1>
             <template_a2> fgh </template_a2>
             <system> op1 </system> 
          </nta>
I want to merge the XMLs and create an output file, that will have the following format:
<nta>
             <declaration> a1, b1,...,n1</declaration>
             <template_a1> abc </template_a1>
             <template_a2> fgh </template_a2>
              ...............................
            <template_n1> klm </template_n1>
            <template_n2> qaz </template_n2>
            <system> op1, op2, ..., opn </system>
        </nta>
I've tried solving this using two different solutions. The first one is this:
for (File listOfFile : listOfFiles ){
 
				Document doc = builder.build(new File(listOfFile));
				//Document doc2 = builder.build(new File("C:\\Users\\V\\Desktop\\SF_antivalent.xml"));
 
				String rootName = doc[0].getRootElement().getName();
				Element newRoot = new Element(rootName);
				Document newDoc = new Document(newRoot);
 
				Element root1[] = doc[i].getRootElement();
				//Element root2 = doc2.getRootElement();
 
		         // creating declaration element by merging the declaration content
				Element declaration = new Element("declaration");
				declaration.addContent(root1[i].getChildText("declaration"));
				//declaration.addContent(root2.getChildText("declaration"));
				newRoot.addContent(declaration); // add declaration element to new document
 
		    		for(int j=0; j < folder[i].length; i++){
 
 
		    			newRoot.addContent(root[j].getChild("template").clone()); 
		                       // directly adding template from document XML1, 
		                      //after getting template child,
		                     //it needs to be cloned to detached  from its parent  
		    		}
		    		//newRoot.addContent(root2.getChild("template").clone()); 
 
		        Element root2[] = doc[i].getRootElement();
 
		    	Element system = new Element("system");
		    	system.addContent(root[i].getChildText("system"));
		    	newRoot.addContent(system);
 
		    	XMLOutputter outputter = new XMLOutputter();
		    	outputter.output((org.jdom2.Document) newDoc, System.out); 
		                  // output the new doc, pass your OutputStream to this function 
				}
 
			System.out.println(newDoc);
			}
 
			catch (Exception v) {
	        	v.printStackTrace();
	        }
in which I get syntax errors and warnings and it doen't work. And the second one is this:
package test;
 
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
 
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.events.XMLEvent;
import javax.xml.transform.stream.StreamSource;
 
import org.jdom2.*;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;
 
 
public class XMLConcat {
    public static void main(String[] args) throws Throwable {
        File dir = new File("C:\\test");
        File[] rootFiles = dir.listFiles();
 
        Writer outputWriter = new FileWriter("C:\\test\\mergedFile.xml");
        XMLOutputFactory xmlOutFactory = XMLOutputFactory.newFactory();
        XMLEventWriter xmlEventWriter = xmlOutFactory.createXMLEventWriter(outputWriter);
        XMLEventFactory xmlEventFactory = XMLEventFactory.newFactory();
 
        xmlEventWriter.add(xmlEventFactory.createStartDocument());
        xmlEventWriter.add(xmlEventFactory.createStartElement("", null, "rootSet"));
 
        XMLInputFactory xmlInFactory = XMLInputFactory.newFactory();
        SAXBuilder builder = new SAXBuilder();
 
        for (File rootFile : rootFiles) {
 
 
            XMLEventReader xmlEventReader = xmlInFactory.createXMLEventReader(new StreamSource(rootFile));
            XMLEvent event = xmlEventReader.nextEvent();     
 
            while (event.getEventType() != XMLEvent.START_ELEMENT) {
                event = xmlEventReader.nextEvent();
            }
 
            do {
                xmlEventWriter.add(event);
                event = xmlEventReader.nextEvent();
            } while (event.getEventType() != XMLEvent.END_DOCUMENT);
            xmlEventReader.close();
 
        }
 
        xmlEventWriter.add(xmlEventFactory.createEndElement("", null, "rootSet"));
        xmlEventWriter.add(xmlEventFactory.createEndDocument());
 
        xmlEventWriter.close();
        outputWriter.close();
    }
 
 
}
which I tested it with a new directory and it works but I don't get the wanted result. Instead of that, I get this result as XML output file:
<nta>
  <declaration> a1 </declaration>
  <template_a1> abc </template_a1>
  <template_a2> fgh </template_a2>
  <system> op1 </system>
</nta>
...............................
<nta>
  <declaration> n1 </declaration>
  <template_n1> klm </template_n1>
  <template_n2> qaz </template_n2>
  <system> opn </system>
</nta>

The number of the files in the list changes every time. How can I solve this problem? Any help is appreciated. Thank you and sorry for the long post.