In einem Folder hab 100 000 und davon wollte 50 000 Dateien zu einer Datein zusammenfügen. Das Code funktioniert mit weniger Mengen, aber mit 50 000 dauert ewig, wie könnte ich mein Code optiemeren. Jede Vorschläge würde mich sehr freuen.
Java:
package com.company;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import java.io.*;
public class XMLReadWriter {
public Document merge(String expression,
File... files) throws Exception {
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
XPathExpression compiledExpression = xpath
.compile(expression);
return merge(compiledExpression, files);
}
public Document merge(XPathExpression expression,
File... files) throws Exception {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
docBuilderFactory
.setIgnoringElementContentWhitespace(true);
DocumentBuilder docBuilder = docBuilderFactory
.newDocumentBuilder();
Document base = docBuilder.parse(files[0]);
Node results = (Node) expression.evaluate(base,
XPathConstants.NODE);
if (results == null) {
throw new IOException(files[0]
+ ": Fehlschlag" + files.length);
}
for (int i = 1; i < files.length; i++) {
Document merge = docBuilder.parse(files[i]);
Node nextResults = (Node) expression.evaluate(merge,
XPathConstants.NODE);
while (nextResults.hasChildNodes()) {
Node kid = nextResults.getFirstChild();
nextResults.removeChild(kid);
kid = base.importNode(kid, true);
results.appendChild(kid);
}
}
return base;
}
public static void print(Document doc) throws Exception {
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory
.newTransformer();
DOMSource source = new DOMSource(doc);
Result result = new StreamResult(System.out);
transformer.transform(source, result);
}
public static void write(Document doc) throws TransformerConfigurationException {
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new StringWriter());
try {
transformer.transform(source, result);
Writer output = new BufferedWriter(new FileWriter("C:/Users/p05865/IdeaProjects/XMLDateien/Materials1Bis50000.xml"));
String xmlOutput = result.getWriter().toString();
output.write(xmlOutput);
output.close();
System.out.println("Ready");
} catch (TransformerException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public File[] findAllFilesInFolder(File... folder) {
return folder;
}
}
Java:
package com.company;
import org.w3c.dom.Document;
import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class Main {
static String path = "C:/Users/p05865/IdeaProjects/muster/Material_1200668137_08092021_104636594.xml";
public static void main(String[] args) throws Exception {
// proper error/exception handling omitted for brevity
com.company.XMLReadWriter xmlReadWriter = new com.company.XMLReadWriter();
File file = new File(path);
File[] files1 = new File[50000];
File[] files = file.listFiles();
int count = 0;
for (int j = 0; j < files.length - 50000; j++){
files1[count++] = files[j];
}
Document doc = xmlReadWriter.merge("/STEP-ProductInformation/Products", files1);
xmlReadWriter.write(doc);
xmlReadWriter.print(doc);
}
}