On this occasion, I share how to create a Java mapping, PI version used is 7.4 (java only). This will help them when they need to create a complex mapping. For this example, use the DOM object, I add sample code.

I will be adding more blog posts, I hope will be helpful.

Steps:


1. Download NWDS (Netweaver Developer Studio) https://nwds.sap.com/swdc/downloads/updates/netweaver/nwds/nw/730/

2. Download JDK (Java Development kit) http://www.oracle.com/technetwork/es/java/javase/downloads/index.html

3. Install Java and NWDS

4. Open NWDS

5. Create new java project

18-09-2015 9-33-31.png

18-09-2015 9-36-32.png

6. Add XPI library

18-09-2015 9-38-32.png

18-09-2015 9-39-59.png

7. Create new Class (Right click on src)

  18-09-2015 9-44-07.png

18-09-2015 10-06-35.png

8. Add code (Example with Dom object)

XML outbound and inbound (Example):

<?xml version=”1.0″ encoding=”UTF-8″?>

<ns0:MT_test xmlns:ns0=”urn:elrosado.com:switchTransaccional:listaNegra”>

  <row>

      <nombre>Juan</nombre>

      <apellido>Padilla</apellido>

      <edad>32</edad>

  </row>

</ns0:MT_test>

18-09-2015 9-59-35.png

Code:

package com.test.mapping;

import java.io.InputStream;

import java.io.OutputStream;

import com.sap.aii.mapping.api.*;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

public class Test_JavaMapping extends AbstractTransformation { 

    @Override

    public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput) throws StreamTransformationException {

        String RESULT = new String();

        String inicio = “<?xml version=\”1.0\” encoding=\”UTF-8\”?>” +

        “<ns0:MT_test xmlns:ns0=\”urn:elrosado.com:switchTransaccional:listaNegra\”>”;

        String fin = “</ns0:MT_test>”;

        String nombre = “”;

        String apellido = “”;

        String edad = “”;

        try {

            InputStream inputstream = transformationInput.getInputPayload().getInputStream(); 

            OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream(); 

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();           

            DocumentBuilder builder = factory.newDocumentBuilder();

  

            Document doc = builder.parse(inputstream);     

            NodeList children = doc.getElementsByTagName(“row”);

  

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

                        Node node = children.item(i);

                    if(node.getNodeType() == Node.ELEMENT_NODE){

                        Element eElement = (Element) node;

                        nombre = eElement.getElementsByTagName(“nombre”).item(0).getTextContent();

                        apellido = eElement.getElementsByTagName(“apellido”).item(0).getTextContent();

                        edad = eElement.getElementsByTagName(“edad”).item(0).getTextContent();

              

                        RESULT = RESULT + “<row>” + “<nombre>” + nombre + ” “ + apellido + “</nombre>” +

                        “<apellido>” + nombre + ” “ + apellido + “</apellido>” + “<edad>” +edad+“</edad>”+“</row>”;

                    }

            }           

  

            RESULT = inicio + RESULT + fin;

outputstream.write(RESULT.getBytes());

        } catch (Exception exception) { 

getTrace().addDebugMessage(exception.getMessage()+ RESULT); 

            throw new StreamTransformationException(exception.toString()+ RESULT); 

        } 

    } 

}

9. Export as jar

18-09-2015 10-20-18.png

18-09-2015 10-21-14.png

18-09-2015 10-23-21.png

10. Upload jar to PI (Enterprise Service Builder)

18-09-2015 10-32-13.png

18-09-2015 10-36-04.png

11. Save and activate

12. Use in Operation mapping

18-09-2015 10-38-51.png

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

Leave a Reply