File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Appending part XML to main XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Appending part XML to main XML" Watch "Appending part XML to main XML" New topic
Author

Appending part XML to main XML

Anjali S Sharma
Ranch Hand

Joined: Jun 29, 2005
Posts: 279
I have an XML in the following format.

The XML content actually comes in the form of a StringBuffer which I convert into DOM object, so that I can parse it.

While parsing this XML whenever any one of these elements is encountered, "fb_info", "fb_input_approve", "fb_response", I call a seperate class (once for each element) and that class returns me another XML. That XML I need to append within the original XML, at the point from where it was called.
Say, <fb_info> is encountered in the code, then a class CreateFeedbackXML is called. This class will do some processing and return an XML say, in the format

Now I want to append it to the original xml (which is converted from StringBuffer to a DOM object), so that it becomes something like

Same way for the other elements.
I am able to call the respective classes and get the part XML but I can't make out how to append it to the original XML so that I get a new XML.
Please guide.
[ November 10, 2008: Message edited by: Anjali S Sharma ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12671
    
    5
When you say:
and that class returns me another XML.

do you mean that it returns a String in XML format, a new DOM, or what?

Seems to me the best approach would be to hand your CreateFeedbackXML the org.w3c.dom.Node that you want new information attached to and have it modify the DOM directly. See the Java API for methods such as appendChild().

Bill


Java Resources at www.wbrogden.com
Anjali S Sharma
Ranch Hand

Joined: Jun 29, 2005
Posts: 279
Originally posted by William Brogden:
When you say:

do you mean that it returns a String in XML format, a new DOM, or what?



it returns XML as a StringBuffer
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12671
    
    5
There is no way to make a DOM accept a String and interpret it as XML nodes to build the modified document you want.

My original suggestion is the way you will have to go.

Bill
Anjali S Sharma
Ranch Hand

Joined: Jun 29, 2005
Posts: 279
I was trying to do the following

once I get the StringBuffer, I convert it to DOM.
but it is giving me an error
Exception in thread "main" org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.
at org.apache.xerces.dom.ParentNode.internalInsertBefore(Unknown Source)


the error is coming at line
childOfFeedback.appendChild(nodeXmlChunck);
Anjali S Sharma
Ranch Hand

Joined: Jun 29, 2005
Posts: 279
Will it help if in the Helper class, I convert StringBuffer to DOM and return it to the main class.
Amer Seifeddine
Ranch Hand

Joined: Apr 25, 2002
Posts: 57
Why not using the power of xmlbeans.. It has cursor which can be moved up down and can insert delete and much more.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12671
    
    5
What part of this error report dont you understand?
Exception in thread "main" org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.
at org.apache.xerces.dom.ParentNode.internalInsertBefore(Unknown Source)


You cant just arbitrarily move a DOM node from one Document to another, so creating another DOM is not the way to go. Like I said before, hand the "helper" class the Node you want new data attached to.

Bill
[ November 11, 2008: Message edited by: William Brogden ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18110
    
    8

Originally posted by William Brogden:
You cant just arbitrarily move a DOM node from one Document to another
And that's why the Document interface has the importNode method, right?
Anjali S Sharma
Ranch Hand

Joined: Jun 29, 2005
Posts: 279
Originally posted by Paul Clapham:
And that's why the Document interface has the importNode method, right?

did this

but it gives an error when using importNode
Anjali S Sharma
Ranch Hand

Joined: Jun 29, 2005
Posts: 279
finally did this
Anjali S Sharma
Ranch Hand

Joined: Jun 29, 2005
Posts: 279
Although importNodes is not throwing an error now but when I print the result
childOfFeedback = document.importNode(nodeXmlChunck, true);
NodeList testchildren = childOfFeedback.getChildNodes();
for(int k=0; k<testchildren.getLength(); k++) {
System.out.println("Yest -"+testchildren.item(k).getNodeName()+" --- "+testchildren.item(k).getTextContent());
}
it shows me only the xml fetched from helper class. What I need is that xml fetched from helper class should be appended (at the right place) to the original XML.
Please guide me on this
Anjali S Sharma
Ranch Hand

Joined: Jun 29, 2005
Posts: 279
I have done this, still there is a problem. Only the xml which is coming from Helper class is getting displayed.


I think there is a problem with this line

childOfFeedback = childOfFeedback.appendChild(importedNode);

Please help
Anjali S Sharma
Ranch Hand

Joined: Jun 29, 2005
Posts: 279
I'll try to explain what I am trying to do. Please ignore my last 4 posts

From the main method of my class, this method called createCompleteXML() will be called. Before this method is called a variable with the name skelotonXML (StringBuffer) would already be set. The value of this (StringBuffer) skeletonXML would be something like



This method called createCompleteXML() would iterate over this skeletonXML (which I have now converted to DOM from StringBuffer) and call relevant classes and methods to create a complete XML. While iterating over this skeletonXML whenever <fb_info>, <fb_response_approval> or <fb_input_approve> element is encountered a class is called. Class to be called for <fb_info> is CreateFeedbackXML, class to be called for <fb_response_approval> is CreateResponseApprovalXML and class to be called for <fb_input_approve> is CreateApproverXML.
All these classes implement the following interface


These implementation classes will return an XML in the form of

This XML needs to be added to the original skelotonXML inside the same element, encountering on which the class was instantiated.
Example,while iterating over skeletonXML, <fb_info> element is encountered. Now getXMLChunck() method of CreateFeedbackXML will be called. This method will return an XML in the form of

This XML I need to append to the original skeleton XML. So that the skeletonXML becomes

Still the code will continue to iterate over the skeleton XML and it will now encounter, <fb_response_approval> element. Now it will call getXMLChunck() method of CreateResponseApprovalXML. This method will return an XML in the form


This XML needs to be appended at the appropriate place in the skeleton XML so that the skeleton XML becomes

This will go on till all the children of fb_responses have been encountered.
This code which I am using for traversing the skeleton XML and appending XML from Helper classes to it is as follows

Please help with this.
[ November 12, 2008: Message edited by: Anjali S Sharma ]
Anjali S Sharma
Ranch Hand

Joined: Jun 29, 2005
Posts: 279
It should have been
childOfFeedback.appendChild(importedNode);
instead of
childOfFeedback = childOfFeedback.appendChild(importedNode);

thank you
Anjali S Sharma
Ranch Hand

Joined: Jun 29, 2005
Posts: 279
Thanks everyone for helping
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Appending part XML to main XML
 
Similar Threads
How to get attribute values of repetitive nodes from an XML
Printing in Array in Ascending order
June Newsletter Puzzle
HashMap with enums
Iterate over DOM object