Solved

Merging two XML documents

Posted on 2002-05-15
8
945 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

786 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