Develop Java Mapping in PI 7.4 Java Only
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
It was very hepful , I didn't know the Developer Studio had the libraries.
It is very helpful. Thank you for nice step by step explanation...
Hi Luis,
Just wanted to confirm, for NWDS version 6 the compatible JDK version will be JDK6 right? the higher version of Java will not be supported?
it very good blog
I requesting you to add output also it will help us.
Hi,
Please tell me what is the need for "Element eElement = (Element) node;" I am not getting this.
Thanks,
Aamir
The Element interface extends the Node interface. "Node" is the kind of objects returned in the getElementsByTagName method though the objects are of type "Element". To access the content of each element we have to use the getElementsByTagName method again, which is not available on Node interface, thus we have to make the cast.
Thank you very much sir