Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

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

6. Add XPI library

7. Create new Class (Right click on src)

 

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>

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

10. Upload jar to PI (Enterprise Service Builder)

11. Save and activate

12. Use in Operation mapping

7 Comments
Labels in this area