DOMノードの移動

ちょっとJavaにて、XMLファイルの操作をしていたのですが、2つのDOMのDocument間でNodeを移動する場合は、一度importNode()してからappendするのですね。知りませんでした。。

            DOMParser parser = new DOMParser();  

            // Create the first document          
            parser.parse(new InputSource(args[0]));
            Document doc1 = parser.getDocument();

            // Create the second document
            parser.parse(new InputSource(args[1]));
            Document doc2 = parser.getDocument();

            // Get root of first document
            Element firstRoot = doc1.getDocumentElement();

            // Get Node to move
            Element secondRoot = doc2.getDocumentElement();
            NodeList kids = secondRoot.getElementsByTagName("game");
            Element oneToMove = (Element)kids.item(0);

            // Add to first document
            firstRoot.appendChild(oneToMove);

ヒント: DOMノードの移動developerWorks) より