Try a cloneNode followed by an adoptNode to use a child element as the root element of a new Document.

    String xml = "<hello><world /></hello>";
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();

        Document document0 = db.parse(new InputSource(new StringReader(xml)));
        Document document1 = db.newDocument();


        NodeList nodeList = document0.getDocumentElement().getChildNodes();
        document1.appendChild(document1.adoptNode(nodeList.item(0).cloneNode(true)));

        // Serialize the two documents to check that the copy was correct.
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(document0), new StreamResult(output));
        System.out.println(output.toString("UTF-8"));

        output.reset();
        transformer.transform(new DOMSource(document1), new StreamResult(output));
        System.out.println(output.toString("UTF-8"));
    } catch (TransformerException e) {
    } catch (IOException e) {
    } catch (SAXException e) {
    } catch (ParserConfigurationException e) {
    }
Answer from Jens on Stack Overflow
🌐
GitHub
github.com › joeferner › node-java
GitHub - joeferner/node-java: Bridge API to connect with existing Java APIs. · GitHub
If you want to play with node-java but don't want to setup the build environment you can run it in docker. ... const java = require('java'); const javaLangSystem = java.import('java.lang.System'); javaLangSystem.out.printlnSync('Hello World');
Author: joeferner
🌐
Aspose
reference.aspose.com › api reference › words › java › com.aspose.words › nodeimporter
NodeImporter | Aspose.Words for Java
January 28, 2026 - Allows to efficiently perform repeated import of nodes from one document to another in Java.
🌐
npm
npmjs.com › package › java
java - npm
October 18, 2025 - Bridge API to connect with existing Java APIs.. Latest version: 0.18.0, last published: a year ago. Start using java in your project by running `npm i java`. There are 193 other projects in the npm registry using java.
      » npm install java
    
Published: Oct 18, 2025
Version: 0.18.0
🌐
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › org › w3c › dom › Node.html
Node (Java Platform SE 8 )
July 21, 2026 - Returns a duplicate of this node, i.e., serves as a generic copy constructor for nodes. The duplicate node has no parent ( parentNode is null) and no user data. User data associated to the imported node is not carried over. However, if any UserDataHandlers has been specified along with the ...
🌐
Chegg
chegg.com › engineering › computer science › computer science questions and answers › import java.io.*;import java.util.*;class node{//definition of node nodedata data; node next; node(nodedata d){ data = d; next = null; }}//end class nodeclass stack{ node top = null; public boolean empty(){ return top == null; }//end empty method public void push(nodedata nd){ node p = new node(nd);
import java.util. - end class Nodeclass Stack{ Node top = null
February 27, 2009 - import java.io.*; import java.util.*; class Node{//definition of node NodeData data; Node next; Node(NodeData d){ data = d; next = null; } }//end class Node class Stack{ Node top = null; public boolean empty(){ return top == null; }//end empty method public void push(NodeData nd){ Node p = new Node(nd); p.next = top; top = p; }//end push public NodeData pop(){ if(this.empty()){ System.out.printf("\nStack Empty"); System.exit(2); } NodeData hold = top.data; top = top.next; return hold; }//end pop }//end class Stack class NodeData{ char ch; int num; NodeData(char c){ ch = c; } NodeData(int n){ n
🌐
GitHub
gist.github.com › jakswa › 298554 › 04007084988e9d708ab18ec6e1952d418e4921a1
Node.java · GitHub
Node.java · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ·
🌐
CodeSpeedy
codespeedy.com › home › implement linked list in java using node class
LinkedList in Java using Node Class - CodeSpeedy
June 3, 2019 - It reaches to the position where we want to add the node by .next and adds. ... //import java.util.LinkedList; public class Runner { public static void main(String[] args) { Linkedlist list=new Linkedlist(); list.insert(18); list.insert(7); list.insert(15); list.insertAtStart(25); list.insertAt(2,55); list.delete(2); list.show(); } }
🌐
Namekdev
namekdev.net › 2016 › 04 › the-state-of-importing-node-js-modules-into-java
The state of importing node.js modules into Java - NamekDev
April 9, 2016 - As I noticed in previous article about script management I’d like to support script sharing in The Console. This topic automatically brings idea of supporting Node.js modules. Nashorn doesn’t provide any kind of module import/export mechanism like CommonJS.
🌐
GitHub
github.com › joeferner › node-java › blob › master › README.md
node-java/README.md at master · joeferner/node-java
If you want to play with node-java but don't want to setup the build environment you can run it in docker. ... const java = require('java'); const javaLangSystem = java.import('java.lang.System'); javaLangSystem.out.printlnSync('Hello World');
Author: joeferner
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-run-java-code-in-node-js
How to Run Java Code in Node.js ? - GeeksforGeeks
June 14, 2024 - Running Java code within a Node.js environment can be useful for integrating Java-based libraries or leveraging Java's robust capabilities within a JavaScript application.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › org › w3c › dom › Document.html
Document (Java Platform SE 7 )
Imports a node from another document to this document, without altering or removing the source node from the original document; this method creates a new copy of the source node. The returned node has no parent; (parentNode is null).