Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 349805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:29:33+00:00 2026-05-12T11:29:33+00:00

I have an XML file (on the left) and I want to create multiple

  • 0

I have an XML file (on the left) and I want to create multiple files (on the right):

<ParentNode>                                  file1:
    <ChildNode>                               <ParentNode>
        <node></node>                             <ChildNode>
    </childNode>                                      <node></node>
    <ChildNode>                                   </childNode>
        <node></node>                         </ParentNode>
    </childNode>                              file2:
    <ChildNode>                               <ParentNode>
        <node></node>                             <ChildNode>
    </childNode>                                      <node></node>
</ParentNode>                                      </childNode>
                                              </ParentNode>

I am trying to take the first child node from the original XML file and add it to a new one but I keep getting errors around replacing nodes.

I want to do something like the following

 DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
 DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
 Document newDocument;

 Node firstChild = document.getFirstChild();
 NodeList childNodes = firstChild.getChildNodes();

 Element parentNode;
 for (int i = 1; i < childNodes.getLength(); i++ ) {
     newDocument = docBuilder.newDocument();
     parentNode = newDocument.createElement("ParentNode");
     newDocument.appendChild(parentNode);
     newDocument.getFirstChild().appendChild(childNodes.item(i));
 }

but I get an error

org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.

any help pointing in the right direction appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T11:29:33+00:00Added an answer on May 12, 2026 at 11:29 am

    From Java documentation,

    Use cloneNode method.

    SUMMARY:

    public Node cloneNode(boolean deep)
    

    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.).

    Cloning an Element copies all
    attributes and their values, including
    those generated by the XML processor
    to represent defaulted attributes, but
    this method does not copy any text it
    contains unless it is a deep clone,
    since the text is contained in a child
    Text node. Cloning an Attribute
    directly, as opposed to be cloned as
    part of an Element cloning operation,
    returns a specified attribute (
    specified is true). Cloning any other
    type of node simply returns a copy of
    this node.

    Note that cloning an immutable subtree
    results in a mutable copy, but the
    children of an EntityReference clone
    are readonly . In addition, clones of
    unspecified Attr nodes are specified.
    And, cloning Document, DocumentType,
    Entity, and Notation nodes is
    implementation dependent.

    EDIT :

    import java.io.*;
    import javax.xml.parsers.*;
    import org.w3c.dom.*;
    import org.xml.sax.*;
    import javax.xml.transform.*; 
    import javax.xml.transform.dom.DOMSource; 
    import javax.xml.transform.stream.StreamResult;
    
    public class Test{
     static public void main(String[] arg) throws Exception{
    
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
     DocumentBuilder builder = factory.newDocumentBuilder();
     Document doc = builder.parse("foo.xml");
    
     TransformerFactory tranFactory = TransformerFactory.newInstance(); 
     Transformer aTransformer = tranFactory.newTransformer(); 
    
    
     NodeList list = doc.getFirstChild().getChildNodes();
    
     for (int i=0; i<list.getLength(); i++){
        Node element = list.item(i).cloneNode(true);
    
     if(element.hasChildNodes()){
       Source src = new DOMSource(element); 
       FileOutputStream fs=new FileOutputStream("k" + i + ".xml");
       Result dest = new StreamResult(fs);
       aTransformer.transform(src, dest);
       fs.close();
       }
       }
    
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file that I'm trying to read from here , and
I want to create an Android ImageButton in the xml file with a background
I have this code in a xml layout file: <reference name=left> <block type=blog/blog name=left.blog.menu
My xml file lists items in left, what I want to change thier positions
I have a xml file as follows <Moves> <Move name=left> <Coord X=100 Y=100/> <Coord
I have xml file with such structure: ... <outer> ... <inner/> ... </outer> ...
What is the easiest way to convert xml to html? I have xml file
I have an XML file from which I am parsing some content to display
I have an XML file that looks like <?xml version=1.0> <playlist> <name>My Playlist</name> <song>
I have a xml file which I need to open with Microsoft Word 2007.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.