5

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>
1
  • Which xml parser are you using? Commented Nov 1, 2011 at 9:11

2 Answers 2

7
  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>
Sign up to request clarification or add additional context in comments.

2 Comments

I try this but with no succces: Document tournament = builder.parse(new File("b.xml")); Document tournaments = builder.parse(new File("a.xml")); Element tournamentElement = (Element) tournament.getFirstChild(); Element ndetournament = (Element) tournaments.getElementsByTagName("turnaje").item(0); Node firstDocImportedNode = tournaments.adoptNode(tournamentElement); ndetournament.appendChild(firstDocImportedNode);
ok thx this work. I didnt have to write changed xml to file With transformerFactory now it is OK
0

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

2 Comments

nope I try this but with no success: Document tournament = builder.parse(new File("b.xml")); Document tournaments = builder.parse(new File("a.xml")); Element tournamentElement = (Element) tournament.getFirstChild(); Element ndetournament = (Element) tournaments.getElementsByTagName("turnaje").item(0); Node firstDocImportedNode = tournaments.importNode(tournamentElement,true); ndetournament.appendChild(firstDocImportedNode);
Where is "b.xml" I've retested using new File("c:\\a.xml") and it seems to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.