追加节点时发生异常-org.w3c.dom.DOMException:WRONG_DOCUMENT_ERR:节点在与创建它的节点不同的其他文档中使用

时间:2018-08-06 06:46:41

标签: java domexception

当我尝试更新节点时,它在给定源代码的粗体行中给出以下异常。 org.w3c.dom.DOMException:WRONG_DOCUMENT_ERR:一个节点在与创建它的文档不同的文档中使用。 这是我的源代码。

private void updateNode(AmmunitionTrace ammunitiontrace)
    {
        NodeList childList = existingGroupNode.getChildNodes();
        for (int i = 0; i < childList.getLength(); i++)
        {
            Node child = childList.item(i);
            switch (child.getNodeName())
            {
                case "Practices":
                    // NodeList practiceGroup = node.getChildNodes();
                    while (child.hasChildNodes())
                    {
                        child.removeChild(child.getFirstChild());
                    }
                    try
                    {
                        for (Practice currentPractice : ammunitiontrace.PRACTICES)
                        {
                            Element practice = document.createElement("Practice");
                            **child.appendChild(practice);**

                            Attr attrPracticeName = document.createAttribute("Name");
                            attrPracticeName.setValue(currentPractice.PRACTICE_NAME);
                            practice.setAttributeNode(attrPracticeName);
                        }

                    }
                    catch (DOMException ex)
                    {
                        Logger.getLogger(AmmoTemplateLibraryEditor.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;
            }
        }
    }

我试图用它代替粗体。

Node importedNode = document.importNode(practice, true);
child.appendChild(importedNode);

但是它不起作用。有人有建议吗?

0 个答案:

没有答案