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

发布于 2024-12-13 02:23:15 字数 922 浏览 0 评论 0原文

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

<tournaments>

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

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

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

</tournaments>

然后我在 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥有 2024-12-20 02:23:15
  1. 获取 Node 从第一个 < a href="http://download.oracle.com/javase/6/docs/api/org/w3c/dom/Document.html" rel="noreferrer">文档;
  2. 采用 Node (参见 Document.adopt(Node)) 从第一个 文档 到第二个 文档
  3. Appent 采用 Node 作为子节点第二个 文档 结构(请参阅节点.appendChild(Node)

更新。
代码:

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. 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>
要走干脆点 2024-12-20 02:23:15

这对你有用吗?

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() 函数以适合您自己的代码

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文