I have list of tournaments in my a.xml:
<tournaments>
<tournament>
<name>a</name>
</tournament>
<tournament>
<name>b</name>
</tournament>
<tournament>
<name>c</name>
</tournament>
</tournaments>
ad then I have one tournament in b.xml
<tournament>
<name>d</name>
</tournament>
How I can apend document b.xml to a.xml into as another tournament ?
so this is what I want:
<tournaments>
<tournament>
<name>a</name>
</tournament>
<tournament>
<name>b</name>
</tournament>
<tournament>
<name>c</name>
</tournament>
<tournament>
<name>d</name>
</tournament>
</tournaments>
Update. Code:
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
Document tournament = builder.parse(new File("b.xml"));
Document tournaments = builder.parse(new File("a.xml"));
Node tournamentElement = tournament.getFirstChild();
Node ndetournament = tournaments.getDocumentElement();
Node firstDocImportedNode = tournaments.adoptNode(tournamentElement);
ndetournament.appendChild(firstDocImportedNode);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(new DOMSource(tournaments), new StreamResult(System.out));
Result:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<tournaments>
<tournament>
<name>a</name>
</tournament>
<tournament>
<name>b</name>
</tournament>
<tournament>
<name>c</name>
</tournament>
<tournament>
<name>d</name>
</tournament>
</tournaments>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With