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 224651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:20:12+00:00 2026-05-11T19:20:12+00:00

I have two XML docs that I’ve created and I want to combine these

  • 0

I have two XML docs that I’ve created and I want to combine these two inside of a new envelope. So I have

<alert-set>
  <warning>National Weather Service...</warning>
  <start-date>5/19/2009</start-date>
  <end-date>5/19/2009</end-date>
</alert-set>

and

 <weather-set>
   <chance-of-rain type="percent">31</chance-of-rain>
   <conditions>Partly Cloudy</conditions>
   <temperature type="Fahrenheit">78</temperature>
 </weather-set>

What I’d like to do is combine the two inside a root node: < DataSet> combined docs < /DataSet>

I’ve tried creating a temporary doc and replacing children with the root nodes of the documents:

<DataSet>
  <blank/>
  <blank/>
</DataSet>

And I was hoping to replace the two blanks with the root elements of the two documents but I get “WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.” I tried adopting and importing the root nodes but I get the same error.

Is there not some easy way of combining documents without having to read through and create new elements for each node?

EDIT: Sample code snippets
Just trying to move one to the “blank” document for now… The importNode and adoptNode functions cannot import/adopt Document nodes, but they can’t import the element node and its subtree… or if it does, it does not seem to work for appending/replacing still.

    Document xmlDoc;     //created elsewhere
    Document weather = getWeather(latitude, longitude);
    Element weatherRoot = weather.getDocumentElement();

    Node root = xmlDoc.getDocumentElement();
    Node adopt = weather.adoptNode(weatherRoot);
    Node imported = weather.importNode(weatherRoot, true);
    Node child = root.getFirstChild();

    root.replaceChild(adopt, child);      //initially tried replacing the <blank/> elements
    root.replaceChild(imported, child);

    root.appendChild(adopt);
    root.appendChild(imported);
    root.appendChild(adopt.cloneNode(true));

All of these throw the DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.

I think I’ll have to figure out how to use stax or just reread the documents and create new elements… That kinda seems like too much work just to combine documents, though.

  • 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-11T19:20:12+00:00Added an answer on May 11, 2026 at 7:20 pm

    It’s a bit tricky, but the following example runs:

    public static void main(String[] args) {
    
        DocumentImpl doc1 = new DocumentImpl();
        Element root1 = doc1.createElement("root1");
        Element node1 = doc1.createElement("node1");
        doc1.appendChild(root1);
        root1.appendChild(node1);
    
        DocumentImpl doc2 = new DocumentImpl();
        Element root2 = doc2.createElement("root2");
        Element node2 = doc2.createElement("node2");
        doc2.appendChild(root2);
        root2.appendChild(node2);
    
        DocumentImpl doc3 = new DocumentImpl();
        Element root3 = doc3.createElement("root3");
        doc3.appendChild(root3);
    
        // root3.appendChild(root1); // Doesn't work -> DOMException
        root3.appendChild(doc3.importNode(root1, true));
    
        // root3.appendChild(root2); // Doesn't work -> DOMException
        root3.appendChild(doc3.importNode(root2, true));   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two XML examples that I want to write a schema for: Example
I have two XML files that are generated by another application I have no
this is my situation: I have two org.w3c.dom.Document created from two xml files. What
I have two applications written in Java that communicate with each other using XML
I want to parse a large XML file and I have two options: Perl
I have have two xml docs: XML1: <Books> <Book id=11> ....... <AuthorName/> </Book> ......
I have two XML sources to retrieve data from. I want to use them
hi i have two xml file and i want to bind grid view eith
I have two xml files that both have the same schema and I would
I have two xml file. I want this two xml file in the same

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.