on 09-11-2014 3:09 PM
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
Hi Rebecca
Have you included StreamTransformation in your java?
import com.sap.aii.mapping.api.StreamTransformation;
Regards
Javi
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Rebecca
You can still try using the StreamTransformation.
com.sap.aii.mapping.api.StreamTransformation
As of SAP NetWeaver NEW YORK, replaced by AbstractTransformation
. Nevertheless this interface is still supported. So it is not necessary to change existing mappings that use this deprecated interface.
Regards
Osman Jabri
Hi Osman,
package packRunJson2Xml;
import java.io.InputStream;
import java.io.OutputStream;
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.xml.XMLSerializer;
import org.apache.commons.io.IOUtils;
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 com.sap.aii.utilxi.core.io.IOUtil;
import org.xml.sax.SAXException;
public class RunJson2Xml extends AbstractTransformation {
public void transform(TransformationInput arg0, TransformationOutput arg1)
throws StreamTransformationException
// {
// arg0.getInputPayload().getInputStream();
// arg1.getOutputPayload().getOutputStream();
// }
// public void execute(InputStream arg0, OutputStream arg1){
{
// String strJSON = convertStreamToString(arg0.getInputPayload().getInputStream());
try
{
getTrace().addInfo("JAVA Mapping Called");
//InputStream is = JSONtoXML.class.getResourceAsStream("JSON.txt");
String strJSON = "";
InputStream arg0 = arg0.getInputPayload().getInputStream();
InputStream.close();
String jsonData = org.apache.commons.io.IOUtils.toString(request.getInputStream());
XMLSerializer serializer = new XMLSerializer();
JSON json = JSONSerializer.toJSON( jsonData );
String xml = serializer.write( json );
//System.out.println(xml);
arg1.getOutputPayload().getOutputStream().write(strJSON.getBytes());
}
catch(Exception ie) { }
}
}
I am trying to include the interface Transformation but I am receiving the following error.
Could you please help me out............
Regards
User | Count |
---|---|
93 | |
10 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.