Dear Experts,
I am working on a scenario to convert the JSON data format as response to XML format in SAP PI 7.4. When I completed the work in NWDS I am receiving no error as it is run as a standalone JAVA application.
After importing in to ESR and running for test I am receiving error as :
Does not implement the required interface com.sap.aii.mapping.api.StreamTransformation or does not enhance the class com.sap.aii.mapping.api.AbstractTransformation.
I followed and implemented from some SCN example but still not able to solve the issue. Below is my code..
package com.cms;
import java.io.RandomAccessFile;
import java.io.StringWriter;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
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 com.cms.model.Item;
import com.cms.model.Items;
import com.cms.model.Response;
import com.fasterxml.jackson.databind.ObjectMapper;
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;
import java.io*;
public class json2xml extends AbstractTransformation {
public void transform(TransformationInput arg0, TransformationOutput arg1)
throws StreamTransformationException
{
arg0.getInputPayload().getInputStream();
arg1.getOutputPayload().getOutputStream();
}
public void execute(InputStream iStream, OutputStream oStream)
Response response = new Response();
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
DOMSource source = null;
try {
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.newDocument();
Element rootElement = doc.createElementNS("http://www.cms.com/items", "Items");
//append root element to document
doc.appendChild(rootElement);
// read source from file system
String parentDirectory = "C:/Users/Desktop/XML2JSON/src/";
String file = "JSON.txt";
RandomAccessFile f = new RandomAccessFile(parentDirectory+file, "r");
byte[] jsondata = new byte[(int)f.length()];
f.read(jsondata);
ObjectMapper objectMapper = new ObjectMapper();
response = objectMapper.readValue(jsondata, Response.class);
//append first child element to root element
rootElement.appendChild(getItem(doc, response.getItems()));
//for output to file, console
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
//for pretty print
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
source = new DOMSource(doc);
//write to console or file
StreamResult result = new StreamResult(new StringWriter());
transformer.transform(source, result);
System.out.println(result.getWriter());
} catch (Exception e) {
e.printStackTrace();
}
}
//utility method to create text node
private static Node getItemElements(Document doc, Element element, String name, String value) {
Element node = doc.createElement(name);
node.appendChild(doc.createTextNode(value));
return node;
}
private static Node getItem(Document doc, List<Items> items) {
Element item = doc.createElement("item");
Item _item = items.get(0).getItem();
item.appendChild(getItemElements(doc, item,"WFID",_item.getWFID()));
item.appendChild(getItemElements(doc, item,"Subject",_item.getSubject()));
item.appendChild(getItemElements(doc, item,"DocumentID",_item.getDocumentID()));
item.appendChild(getItemElements(doc, item,"Sender",_item.getSender()));
item.appendChild(getItemElements(doc, item,"SenderDomainName",_item.getSenderDomainName()));
item.appendChild(getItemElements(doc, item,"Action",_item.getAction()));
item.appendChild(getItemElements(doc, item,"Date",_item.getDate()));
item.appendChild(getItemElements(doc, item,"DocumentClassID",_item.getDocumentClassID()));
item.appendChild(getItemElements(doc, item,"Comment",_item.getComment()));
item.appendChild(getItemElements(doc, item,"WobNum",_item.getWobNum()));
item.appendChild(getItemElements(doc, item,"MainSource",_item.getMainSource()));
item.appendChild(getItemElements(doc, item,"SubSource",_item.getSubSource()));
item.appendChild(getItemElements(doc, item,"PriorityLevel",_item.getPriorityLevel()));
item.appendChild(getItemElements(doc, item,"IsGroupMail",_item.getIsGroupMail()));
item.appendChild(getItemElements(doc, item,"IsOpen",_item.getIsOpen()));
item.appendChild(getItemElements(doc, item,"DaysCount",_item.getDaysCount()));
return item;
}
}
Please help to rectify the code error?
Regards