Skip to end of metadata
Go to start of metadata

Author: Santhosh Kumar V
Submitted: 04.08.2008
Related Links:

Java Code to Convert the Complete Input XML to String.

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Element;
import org.w3c.dom.Document;
import org.w3c.dom.Text;
import com.sap.aii.mapping.api.StreamTransformation;
/**
 * @author santhosh.vellingiri
 */
public class xmlpayload implements StreamTransformation {
 public static void main(String args[]) throws Exception {
  FileInputStream inFile =
   new FileInputStream("C:/Documents and Settings/santhosh.vellingiri/Desktop/SGD/xmlin.XML");
  FileOutputStream outFile =
   new FileOutputStream("C:/Documents and Settings/santhosh.vellingiri/Desktop/SGD/xmlout.XML");
  xmlpayload xml = new xmlpayload();
  xml.execute(inFile, outFile);
  System.out.println("Success");
 }
 public void setParameter(Map param) {
  Map map = param;
 }
 public void execute(InputStream in, OutputStream out)
  throws com.sap.aii.mapping.api.StreamTransformationException {
  try {
   //************************Code To Generate The XML Parsing Objects*****************************//     
   DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
   DocumentBuilder db = dbf.newDocumentBuilder();
   TransformerFactory tf = TransformerFactory.newInstance();
   Transformer transform = tf.newTransformer();
   
   //Document doc = db.parse(in);
   
   Document docout = db.newDocument();
   Element root = docout.createElement("MT_Trg");
   root.setAttribute("xmlns:ns","urn:Test_File_to_File");
   docout.appendChild(root);
   Element stringinp = docout.createElement("stringinp");
   root.appendChild(stringinp);
   
   BufferedReader inpxml = new BufferedReader(new InputStreamReader(in));
   StringBuffer buffer = new StringBuffer();
   String line="";
   while ((line = inpxml.readLine()) != null)
   {
   buffer.append(line);
   }
   String inptxml=buffer.toString();
   
   Text srcxml = docout.createTextNode(inptxml);
    
   stringinp.appendChild(srcxml);
   DOMSource domS = new DOMSource(docout);
   transform.transform((domS), new StreamResult(out));
   
   
   } catch (Exception e) {
    System.out.print("Problem parsing the file: " + e.getMessage());
    e.printStackTrace();
   }
  }
}