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