How to add node from an XML document to an existing xml document

I have a 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 can I attach a b.xml document to a.xml as another tournament?

here 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> 
+4
source share
2 answers

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> 
+6
source

Will this work for you?

 import java.io.StringBufferInputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; public class Tournament { private static final String tournamentData = " <tournaments>" + " <tournament>" + " <name>a</name>" + " </tournament>" + " <tournament>" + " <name>b</name>" + " </tournament>" + " <tournament>" + " <name>c</name>" + " </tournament>" + "</tournaments>"; private static final String tournamentB = " <tournament>" + " <name>d</name>" + " </tournament>"; private static DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); public static void main(String[] args) { try { Document currentTournaments = getCurrentTournaments(); Element tournament = getNewTournament(); Element ndetournament = (Element) currentTournaments.getElementsByTagName("tournaments").item(0); Node firstDocImportedNode = currentTournaments.importNode(tournament, true); ndetournament.appendChild(firstDocImportedNode); } catch (Exception e) { e.printStackTrace(); } } private static Document getCurrentTournaments() throws Exception{ DocumentBuilder builder = dbFactory.newDocumentBuilder(); Document docTournament = builder.parse(new StringBufferInputStream(tournamentData)); return docTournament; } private static Element getNewTournament() throws Exception{ DocumentBuilder builder = dbFactory.newDocumentBuilder(); Document newTournament = builder.parse(new StringBufferInputStream(tournamentData)); Element tournament = newTournament.getDocumentElement(); return tournament; } } 

You can compensate for the getXXXX () function set with your own code.

0
source

Source: https://habr.com/ru/post/1379101/


All Articles