开发者

How can I copy an node to a different Document without the DOMException?

In My App, I want to copy a node from one document to another .

Here is the code.

Element entryElement, operationElement;
for (int i = 0; i < eventList.getLength(); i++) {
    entryElement = (Element) eventList.item(i);
    gsTag = entryElement.getAttribute("gs_tag");
    System.out.println(gsTag);
    if (gsTag.equals("insert") || gsTag.equals("update") || gsTag.equals("delete")) {
        entryElement =  (Element) entryElement.cloneNode(true);
        rootElement.appendChild(entryElement);
        operationElement = syncDom.createElement("batch:operation开发者_Go百科");
        entryElement.appendChild(operationElement);
        operationElement.setAttribute("type", gsTag);
    }
}

when doing rootElement.appendChild(entryElement); the domexception occurs , and I checked the exception's code is 4. In java API , this code means one element can only belong to one document, but I did already clone the node. Why did it happen?


Cloning the node doesn't change which document it belongs to, because it is an exact copy of the node. To add a node to a different document, you need to have the target document 'adopt' it, using the "importNode" method of Document:

http://download.oracle.com/javase/1.4.2/docs/api/org/w3c/dom/Document.html#importNode%28org.w3c.dom.Node,%20boolean%29

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜