Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 8368

Re: Need Java Mapping Code to perform structure transformation in SAP PI 7.31 !!!

$
0
0

Hi Neha

 

This is the complete java mapping code. Hope this will help you

 

package com.sap;

 

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
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.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;

 

public class javaMapforSCN extends AbstractTransformation {

 

public void transform(TransformationInput arg0, TransformationOutput arg1)
   throws StreamTransformationException {
 
  try {


   DocumentBuilderFactory tfactory = DocumentBuilderFactory
     .newInstance();

   DocumentBuilder tbuilder = tfactory.newDocumentBuilder();

   Document doc = tbuilder.parse(arg0.getInputPayload().getInputStream());

   Document newdoc = tbuilder.newDocument();

   Element root = (Element) newdoc.createElement("action");

   newdoc.appendChild(root);

   NodeList nlList = doc.getElementsByTagName("inputs");

   for (int i = 0; i < nlList.getLength(); i++) {

    Node data = nlList.item(i).getChildNodes().item(1);

    String deviceVal = data.getAttributes().item(0).getNodeValue();
    String setVal = data.getAttributes().item(1).getNodeValue();

    Element outs = (Element) newdoc.createElement("outputs");

    root.appendChild(outs);

    Element ins = (Element) newdoc.createElement("ouput");

    outs.appendChild(ins);

    ins.setAttribute("name", deviceVal);
    ins.setAttribute("set", setVal);
   }

   Transformer transformer = TransformerFactory.newInstance()
     .newTransformer();
   Source source = new DOMSource(newdoc);
   Result output = new StreamResult(arg1
     .getOutputPayload().getOutputStream());
   transformer.transform(source,output);

  } catch (Exception e) {
   e.printStackTrace();
  }

}// end of transform

 

 

}

 

Input:

pic1.jpg

 

Output:

 

pic2.jpg


Viewing all articles
Browse latest Browse all 8368


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>