cancel
Showing results for 
Search instead for 
Did you mean: 

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

Former Member
0 Kudos

Hi All,

I am working on a real time EWM Scenario wherein I have to transform a light off file from one structure to another with value mapping too.

Example:

Source Structure:


<?xml version="1.0" encoding="UTF-8"?>

<action>

<inputs>

<input high="false" device="2.1.11"/>

</inputs>

</action>

Target Structure:

<?xml version="1.0" encoding="UTF-8"?>

<action>

<outputs>

<output name="2.1.5" set="false"/>

</outputs>

</action>

Now, I need to map device values from source structure to name values in target structure. For this i have predefined table with corresponding values.

Can anybody let me know, how i can get "device" values in my Java mapping and use the same to get name and set attributes in target structure.

Thanks

Neha

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

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:

Output:

Former Member
0 Kudos

Hi Neha

Following piece of code will provide you the value of device which is '2.1.11'

try
          {


DocumentBuilderFactory tfactory = DocumentBuilderFactory
    .newInstance();

  DocumentBuilder tbuilder = tfactory.newDocumentBuilder();

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

    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();
    
    
    }
   
     }
          catch(Exception e)
          {
               e.printStackTrace();
          }

iaki_vila
Active Contributor
0 Kudos

Hi Neha,

If you want to call a value-mapping form java code you can check this document http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/00ee347e-aabb-2a10-b298-d15a1ebf4...(Page 14)

Regards.