Solved

Merging two XML documents

Posted on 2002-05-15
8
924 Views
Last Modified: 2012-06-21
I am using the xerces DOMParser with Java.  I want to be able to take the top level node from one document
and append or insert it into a Node on a second, different Document.  When I try to simply call appendChild I get an exception that says "Wrong Document".  Is there a simply way of doing this?
0
Comment
Question by:c095276
8 Comments
 

Expert Comment

by:kamaljeets
ID: 7010768
can u give the stack trace of the exception, then it will be easy to help.
-kjs
0
 

Author Comment

by:c095276
ID: 7010788
org.w3c.dom.DOMException: DOM005 Wrong document
     at org.apache.xerces.dom.ParentNode.internalInsertBefore(ParentNode.java:393)
     at org.apache.xerces.dom.ParentNode.insertBefore(ParentNode.java:325)
     at org.apache.xerces.dom.NodeImpl.appendChild(NodeImpl.java:226)
     at com.wachovia.cmg.ciwt.common.xml.XMLManager.appendNode(XMLManager.java:187)
     at com.wachovia.cmg.ciwt.workflow.upes.UPESManager.createProcessContainerFromXML(UPESManager.java:239)
     at com.wachovia.cmg.ciwt.workflow.upes.UPESMain.main(UPESMain.java:48)
Exception in thread "main"
0
 

Author Comment

by:c095276
ID: 7010798
Here is a code snippet I have removed all the unnecessary stuff such as exception handling

// missing navigate parent document to correct node set to parentNode

InputSource xmlSource = new InputSource(new StringReader(xmlStr));

DOMParser strParser = new DOMParser();          

Document newXMLDoc = strParser.parse(xmlSource);

Node nodeToBeInserted = newXMLDoc.getFirstChild();
                         
parentNode.appendChild( nodeToBeInserted);
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 16

Accepted Solution

by:
heyhey_ earned 200 total points
ID: 7010813
http://www.google.com/search?q=DOM005+Wrong+document&sourceid=opera&num=0&ie=utf-8&oe=utf-8

voids to

http://www-106.ibm.com/developerworks/xml/library/x-tipmvdom.html

which describes solution to your problem

  // Add to first document
            Node newOneToMove = doc1.importNode(oneToMove, true);
            firstRoot.appendChild(newOneToMove);
        } catch (Exception e) {
0
 

Author Comment

by:c095276
ID: 7010834
What is the type of doc1?  I have an object of type org.w3c.dom.Document and I do not have an importNode fucntion.
0
 
LVL 35

Expert Comment

by:girionis
ID: 7010846
>  I have an object of type org.w3c.dom.Document and I do not have an importNode
fucntion.

  Are you sure you are using the latest Java XML APIs?

P.S. I think the moderators should add a "quote" function for replies. It would be quite handy.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8749725
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- points to heyhey_

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now