如何将节点从xml文档附加到现有的xml文档

时间:2011-11-01 09:05:56

标签: java xml dom

我的a.xml中有锦标赛列表:

<tournaments>

    <tournament>
        <name>a</name>
    </tournament>

    <tournament>
        <name>b</name>
    </tournament>

    <tournament>
        <name>c</name>
    </tournament>

</tournaments>

ad然后我在b.xml中有一个锦标赛

<tournament>
    <name>d</name>
</tournament>

如何将b.xml文档作为另一个锦标赛添加到a.xml中?

所以这就是我想要的:

<tournaments>

    <tournament>
        <name>a</name>
    </tournament>

    <tournament>
        <name>b</name>
    </tournament>

    <tournament>
        <name>c</name>
    </tournament>

    <tournament>
        <name>d</name>
    </tournament>

</tournaments>

2 个答案:

答案 0 :(得分:6)

  1. Node;
  2. 添加Document
  3. 从第一个Node到第二个Document.adopt(Node)采用Document(请参阅Document);
  4. 作为第二个Node结构的孩子,通过了Document,(Node.appendChild(Node)
  5. 更新。 代码:

    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));
    

    结果:

    <?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>
    

答案 1 :(得分:0)

这对你有用吗?

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;
  }
}

您可以修改getXXXX()函数套件自己的代码