hudi
hudi

Reputation: 16525

how to append node from xml document to existing xml document

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>

Upvotes: 5

Views: 6629

Answers (2)

svaor
svaor

Reputation: 2245

  1. Get Node to add from first Document;
  2. Adopt Node (see Document.adopt(Node)) from first Document to the second Document;
  3. Appent adopted Node as a child to second Document structure (see Node.appendChild(Node).

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>

Upvotes: 7

GrahamA
GrahamA

Reputation: 5923

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 ammend the getXXXX() functons suite your own code

Upvotes: 0

Related Questions