Main program for the DOM parser Java Mapping

Hi Folks,

I am not a regular java programmer and I have a Java mapping which actually merges 2 nput XMLs (One is the main payload and the other is the application attachment). The following is the code where "Attachment1" is the root node of the second XML which is being merged. I would like to test the code for multiple attachments, i.e I would like to to see if the code can merge more than 2 XMLs (One being the Main payload and the other 2 being the attachments like for example Attachment 1 and Attachment 2). In order to debug the code I know I need a main program. I would need help in writing the same. Please suggest. Also FYI, in the code Transform method is something I need to use it to make the Java mapping work in my middle ware tool for message transformations.

Also PFA are the sample xml files to be merged.

package fileMerge;

import java.util.*;  
import javax.xml.parsers.*;  
import javax.xml.transform.*;  
import javax.xml.transform.dom.DOMSource;  
import org.w3c.dom.*;  
import org.xml.sax.SAXException;  

public class MergeAttachment extends AbstractTransformation{  
  public void transform(TransformationInput in, TransformationOutput out)  throws StreamTransformationException  
  InputAttachments inputAttachments = in.getInputAttachments();  
  InputStream inputstream = in.getInputPayload().getInputStream();  
  OutputStream outputstream = out.getOutputPayload().getOutputStream();  
  try {  
  DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();  
  DocumentBuilder dbuilder = dbfactory.newDocumentBuilder();  
  Document mainDoc = dbuilder.parse(inputstream);  
  Collection<String> collectionIDs  = inputAttachments.getAllContentIds(true);  
  Object[] arrayObj  =collectionIDs.toArray();  
  for(int i=0;i<arrayObj.length;i++)  
  String attachmentID = (String) arrayObj[i];  
  Attachment attachment =inputAttachments.getAttachment(attachmentID);  
  InputStream attachmentInputStream = new ByteArrayInputStream(attachment.getContent());  
  Document attachmentDoc = dbuilder.parse(attachmentInputStream);  
  Element element = (Element) attachmentDoc.getElementsByTagName("Attachment1").item(0);  
  Node copiedNode = mainDoc.importNode(element, true);  
  TransformerFactory tf = TransformerFactory.newInstance();  
  Transformer transformer  = tf.newTransformer();  
  transformer.transform(new DOMSource(mainDoc), new StreamResult(outputstream));  
  } catch (ParserConfigurationException e) {  
  this.getTrace().addInfo("ParserConfigurationException caught");  
  } catch (SAXException e) {  
  this.getTrace().addInfo("SAXException caught");  
  } catch (IOException e) {  
  this.getTrace().addInfo("IOException caught");  
  } catch (TransformerException e) {  
  this.getTrace().addInfo("TransformerException caught");  

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kalyangkmAuthor Commented:
A small correction to the Main payload attachment. PFA is the correct one.
Amitkumar PSr. ConsultantCommented:
Please refer this link.

It contains a main() method in which object of TransformationInput and TransformationOutput created and passed to tranform() method. I think this is what you are looking for.

Hope it helps.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kalyangkmAuthor Commented:
Thanks Amit.

It helps.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.