首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将xml文档中节点追加到已有xml文档中

如何将xml文档中节点追加到已有xml文档中
EN

Stack Overflow用户
提问于 2011-11-01 17:05:56
回答 2查看 6.4K关注 0票数 5

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

代码语言:javascript
复制
<tournaments>

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

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

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

</tournaments>

广告然后我在b.xml有一个锦标赛

代码语言:javascript
复制
<tournament>
    <name>d</name>
</tournament>

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

这就是我想要的:

代码语言:javascript
复制
<tournaments>

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

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

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

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

</tournaments>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-01 17:40:37

从first Document;

  • Adopt

  • (请参见Node )获取要从first Document

  • Adopt添加到second Document;

  • Appent的Node Node (请参见Document.adopt(Node))作为第二个Document结构的子项采用的Node (请参见Node.appendChild(Node).

更新。代码:

代码语言:javascript
复制
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));

结果:

代码语言:javascript
复制
<?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>
票数 7
EN

Stack Overflow用户

发布于 2011-11-01 18:05:37

这对你有用吗?

代码语言:javascript
复制
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()函数以适合您自己的代码

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7964357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档