Results 1 to 1 of 1
Thread: Appending part XML to main XML
- 11-13-2008, 05:23 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 4
- Rep Power
- 0
Appending part XML to main XML
I am trying to append one XML within another XML but unable to do so. Can someone please help me with this.
I'll try to explain what I am trying to do.
From the main method of my class, a 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.Java Code:<feedback> <fb_info> <fb_id>27006971</fb_id> </fb_info> <fb_responses> <fb_response_approval> <res_id/> </ fb_response_approval > <fb_input_approve> <res_id/> </ fb_input_approve > </fb_responses> </feedback>
All these classes implement the following interface
These implementation classes will return an XML in the form ofJava Code:public interface IXMLCreator { public StringBuffer getXMLChunck(String id, IDfSession session); }
This XML needs to be added to the original skelotonXML inside the same element, encountering on which the class was instantiated.Java Code:<fb123> <feedback_id><![CDATA[27006971]]></feedback_id> <operation_division/> <fb_brand><![CDATA[ert]]></fb_brand> <fb_url/> </fb123>
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 becomesJava Code:<fb123> <feedback_id><![CDATA[27006971]]></feedback_id> <operation_division/> <fb_brand><![CDATA[ert]]></fb_brand> <fb_url/> </fb123>
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 formJava Code:<feedback> <fb_info> <fb123> <feedback_id><![CDATA[27006971]]></feedback_id> <operation_division/> <fb_brand><![CDATA[ert]]></fb_brand> <fb_url/> </fb123> </fb_info> <fb_responses> <fb_response_approval> <res_id/> </ fb_response_approval > <fb_input_approve> <res_id/> </ fb_input_approve > </fb_responses> </feedback>
This XML needs to be appended at the appropriate place in the skeleton XML so that the skeleton XML becomesJava Code:<fb456> <feedback_id><![CDATA[2700633222]]></feedback_id> <fb_brand><![CDATA[eeee]]></fb_brand> <fb_url/> </fb456>
This will go on till all the children of fb_responses have been encountered.Java Code:<feedback> <fb_info> <fb123> <feedback_id><![CDATA[27006971]]></feedback_id> <operation_division/> <fb_brand><![CDATA[ert]]></fb_brand> <fb_url/> </fb123> </fb_info> <fb_responses> <fb_response_approval> <fb456> <feedback_id><![CDATA[2700633222]]></feedback_id> <fb_brand><![CDATA[eeee]]></fb_brand> <fb_url/> </fb456> </ fb_response_approval > <fb_input_approve> <res_id/> </ fb_input_approve > </fb_responses> </feedback>
This code which I am using for traversing the skeleton XML and appending XML from Helper classes to it is as follows
After appending, when I try to print the dom object usingJava Code:public void createCompleteXML() { try { // create a new DocumentBuilder object DocumentBuilder builder = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = builder.parse(new org.xml.sax.InputSource(new StringReader(skeletonXml.toString()))); Element root = document.getDocumentElement(); System.out.println("The root element is " + root.getNodeName() + ".\n"); NodeList feedbackNodeList = document.getElementsByTagName("fb_info"); for(int i=0; i<feedbackNodeList.getLength(); i++) { Node childOfFeedback = feedbackNodeList.item(i); if (childOfFeedback.getNodeType() == childOfFeedback.ELEMENT_NODE) { String strChildOfFeedback = childOfFeedback.getNodeName(); System.out.println(childOfFeedback.getNodeName()+" : "+childOfFeedback.getNodeValue()); if(strChildOfFeedback.equalsIgnoreCase("fb_info")) { IXMLCreator cfxml = new CreateFeedbackXML(); StringBuffer xmlChunck = cfxml.getXMLChunck(feedback_id, session); System.out.println("fb id from XML is : "+childOfFeedback.getChildNodes().item(0).getTextContent()); System.out.println("xml chunck: "+xmlChunck); Document docXmlChunck = builder.parse(new org.xml.sax.InputSource(new StringReader(xmlChunck.toString()))); Node nodeXmlChunck = docXmlChunck.getDocumentElement(); Node importedNode = document.importNode(nodeXmlChunck, true); childOfFeedback = childOfFeedback.appendChild(importedNode); NodeList testchildren = childOfFeedback.getChildNodes(); for(int k=0; k<testchildren.getLength(); k++) { System.out.println("Yest -"+testchildren.item(k).getNodeName()+" --- "+testchildren.item(k).getTextContent()); } System.out.println("child of fb -----------++++++ : "+childOfFeedback.toString()); } } } NodeList responsesNodeList = document.getElementsByTagName("fb_responses"); Node responsesNode = responsesNodeList.item(0); for (Node child = responsesNode.getFirstChild();child != null;child = child.getNextSibling()) { if (child.getNodeType() == child.ELEMENT_NODE) { System.out.println("-----"+child.getNodeName()); String strChildOfResponses = child.getNodeName(); if(strChildOfResponses.equalsIgnoreCase("fb_input_approve")) { IXMLCreator cfxml = new CreateApproverXML(); String response_id=child.getChildNodes().item(0).getTextContent(); System.out.println("Response id from Approver XML is : "+response_id); StringBuffer xmlChunck = cfxml.getXMLChunck(response_id, session); System.out.println("approver xml chunck: "+xmlChunck); // Document docXmlChunck = builder.parse(new org.xml.sax.InputSource(new StringReader(xmlChunck.toString()))); /*Node nodeXmlChunck = docXmlChunck.getDocumentElement(); childOf = 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()); }*/ } } } } catch (org.xml.sax.SAXException e) { e.printStackTrace(); // or throw a Exception } catch (java.io.IOException e) { e.printStackTrace(); // or throw a Exception } catch (javax.xml.parsers.ParserConfigurationException e) { e.printStackTrace(); // or throw a Exception } }
for(int k=0; k<testchildren.getLength(); k++) {
System.out.println("Yest -"+testchildren.item(k).getNodeName()+" --- "+testchildren.item(k).getTextContent());
}
I see only the xml which was returned by helper class and not the complete xml.
Can some expert please help me with this
Similar Threads
-
[SOLVED] Why main() in java is declared as public static void main?
By piyu.sha in forum New To JavaReplies: 5Last Post: 10-06-2008, 12:11 AM -
Appending to ArrayList
By eva in forum New To JavaReplies: 2Last Post: 01-24-2008, 04:37 PM -
How to get part of a String?
By eva in forum New To JavaReplies: 1Last Post: 12-23-2007, 06:58 AM -
ERROR: Exception in thread "main" java.lang.NoSuchMethodError: main
By barney in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:10 AM -
exception in thred main java.lang.nosuchmethoderror: main
By fernando in forum Java AppletsReplies: 1Last Post: 08-06-2007, 09:11 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks