많은 xml 파일을 하나로 병합하려고합니다. DOM에서 성공적으로 수행했지만이 솔루션은 몇 개의 파일로 제한됩니다. 여러 파일> 1000에서 실행하면 java.lang.OutOfMemoryError가 발생합니다. 나는 다음과 같은 파일이 어디> 1000 xml 파일을 Java를 사용하여 하나로 병합하는 방법
는 내가 달성하고자하는 것은
파일 1 :
<root>
....
</root>
파일 2 :
<root>
......
</root>
파일 N :
<root>
....
</root>
결과 : 출력 :
<rootSet>
<root>
....
</root>
<root>
....
</root>
<root>
....
</root>
</rootSet>
이 내 현재의 구현 :
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element rootSetElement = doc.createElement("rootSet");
Node rootSetNode = doc.appendChild(rootSetElement);
Element creationElement = doc.createElement("creationDate");
rootSetNode.appendChild(creationElement);
creationElement.setTextContent(dateString);
File dir = new File("/tmp/rootFiles");
String[] files = dir.list();
if (files == null) {
System.out.println("No roots to merge!");
} else {
Document rootDocument;
for (int i=0; i<files.length; i++) {
File filename = new File(dir+"/"+files[i]);
rootDocument = docBuilder.parse(filename);
Node tempDoc = doc.importNode((Node) Document.getElementsByTagName("root").item(0), true);
rootSetNode.appendChild(tempDoc);
}
}
내가 XSLT, 색소폰과 많은 실험을했다,하지만 난 뭔가를 놓치고 유지하는 것 같다. 어떤 도움을 주시면 감사하겠습니다.
실제로 DOM을 메모리에 보관해야하는 이유가 있습니까? 이 경우 간단한 문자열 연결 이상을 필요로합니까? –
단순 연결은 각 개별 xml 파일이 병합되는 경우 xml 선언을 유지합니다. 사실 원칙적으로 xml 파일의 간단한 연결을 찾고 있습니다. – Andra
왜 여러 XML 파일을 하나의 아카이브에 저장하지 않습니까? 그것은 하나의 파일로 끝납니다. 읽기/쓰기 속도가 중요하고 파일 크기 나 대역폭이 더 중요한 경우 압축하면 압축되지 않습니다. –