Recent project required make call to Concur REST API using PI REST adapter, get REST XML response, then do further processing. The REST XML response do not conform with message type/external definition defined, cause problem not able to process further in operation mapping and message mapping.
Below is coding written in message mapping, follow this concept “Write Java Mapping directly in ESR!“
Brief explanation what it does:
Step 1 : Get source xml into inDoc using DOM method.
Step 2 : Get inDoc’s element “job”, remove unnecessary attributes.
Step 3 : Copy inDoc’s root to copiedRoot importNode function
Step 4 : Create new outDoc, add root element “ns0:ED_CONCUR_Job_Status_Response”
Step 5 : then add copiedRoot to outDoc’s root.
Step 6 : Generate target xml using transformer.transform function.
public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput) throws StreamTransformationException{
InputStream inputstream = transformationInput.getInputPayload().getInputStream();
OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream();
execute(inputstream, outputstream);
}
public void execute(InputStream inputstream, OutputStream outputstream) {
try{
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document inDoc = docBuilder.parse(inputstream);
Document outDoc = docBuilder.newDocument();
Element root;
root = outDoc.createElement("ns0:ED_CONCUR_Job_Status_Response");
root.setAttribute("xmlns:ns0","urn:yourcompany.com:YourProcess:YourSubProcess");
Element element = (Element)inDoc.getElementsByTagName("job").item(0);
element.removeAttribute("xmlns");
element.removeAttribute("xmlns:i");
Node inRoot = inDoc.getDocumentElement();
Node copiedRoot = outDoc.importNode(inRoot, true);
outDoc.appendChild(root);
root.appendChild(copiedRoot);
javax.xml.transform.Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource(outDoc), new StreamResult(outputstream));
}catch (Exception e){
e.printStackTrace();
this.getTrace().addDebugMessage(e.toString());
}
}
import added:
com.sap.aii.mapping.api.*
com.sap.aii.mapping.lookup.*
com.sap.aii.mappingtool.tf7.rt.*
java.io.*
java.lang.reflect.*
java.util.*
javax.xml.parsers.*
javax.xml.transform.*
javax.xml.transform.dom.*
javax.xml.transform.stream.*
org.w3c.dom.Document
org.w3c.dom.NodeList
org.w3c.dom.Node
org.w3c.dom.Element
Done!