Как я копирую узлы DOM от одного документа до другого в Java?

Нет, это невозможно. Взгляните на Webhook Documentation of Discord . Когда вы немного опуститесь, вы увидите, что можно отправить с полезной нагрузкой

.

30
задан Jherico 17 April 2013 в 15:58
поделиться

2 ответа

The problem is that Node's contain a lot of internal state about their context, which includes their parentage and the document by which they are owned. Neither adoptChild() nor importNode() place the new node anywhere in the destination document, which is why your code is failing.

Since you want to copy the node and not move it from one document to another there are three distinct steps you need to take...

  1. Create the copy
  2. Import the copied node into the destination document
  3. Place the copied into it's correct position in the new document
for(Node n : nodesToCopy) {
    // Create a duplicate node
    Node newNode = n.cloneNode(true);
    // Transfer ownership of the new node into the destination document
    newDoc.adoptNode(newNode);
    // Make the new node an actual item in the target document
    newDoc.getDocumentElement().appendChild(newNode);
}

The Java Document API allows you to combine the first two operations using importNode().

for(Node n : nodesToCopy) {
    // Create a duplicate node and transfer ownership of the
    // new node into the destination document
    Node newNode = newDoc.importNode(n, true);
    // Make the new node an actual item in the target document
    newDoc.getDocumentElement().appendChild(newNode);
}

The true parameter on cloneNode() and importNode() specifies whether you want a deep copy, meaning to copy the node and all it's children. Since 99% of the time you want to copy an entire subtree, you almost always want this to be true.

71
ответ дан 27 November 2019 в 23:23
поделиться

acceptChild не создает дубликат, он просто перемещает узел другому родителю.

Возможно, вам понадобится метод cloneNode ().

4
ответ дан 27 November 2019 в 23:23
поделиться
Другие вопросы по тегам:

Похожие вопросы: