on 12-21-2012 6:30 PM
Hi Guys!! I have a requirement from SAP to WS is syncronous, I need the code for mapping response using Java mapping. I attached the source and target, file1ws and file2rfc, respectively.
The error message that obtain at sender Channel is
The Java code used is attached.
Regards.
Hi Ernesto,
The file file2rfc.xml you uploaded is not the correct one. The web browser parser is failing to parse the document.
Regards
Anupam
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you all for the contributions. Following the post, I show capture what we are missing in the message destination, is highlighted in yellow.
Updated Java code and the message source, were attached.
As seen in the screenshot, we need to add a root node with a namespace. Any contribution is welcome.
Regards.
Yes Eduardo. I just need to add the tag in the Java Mapping <ZDM_RFC_REPLICA_CONVENIOS_PI.Response> and do not know how.
The code used is:
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
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.NodeList;
import org.xml.sax.InputSource;
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;
public class string_to_xml extends AbstractTransformation {
@Override
public void transform(TransformationInput in, TransformationOutput out)
throws StreamTransformationException {
String inData = "";
try {
getTrace().addInfo("JAVA Mapping Iniciado");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document docIn = db.parse(in.getInputPayload().getInputStream());
NodeList nodos = docIn.getChildNodes();
if (nodos.item(0) != null) {
String xmlEmbebido = nodos.item(0).getTextContent();
InputSource source = new InputSource(new StringReader(
xmlEmbebido));
Document docOut = db.parse(source);
inData = getStringFromDocument(docOut);
inData=inData.replaceAll("XML_OUTPUT", "E_RESULTADO");
inData=inData.replaceAll("COD_RECHAZO", "CODIGO_RECHAZO");
}
} catch (Exception e) {
getTrace().addInfo("Excepcion: " + e.getMessage());
}
try {
out.getOutputPayload().getOutputStream().write(
inData.getBytes("UTF-8"));
getTrace().addInfo("JAVA Mapping Finalizado");
} catch (IOException e1) {
getTrace().addInfo("IOException e1: " + e1.getMessage());
}
}
public String getStringFromDocument(Document doc) {
try {
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return writer.toString();
} catch (TransformerException ex) {
getTrace().addInfo("TransformerException e1: " + ex.getMessage());
return null;
}
}
}
Regards.
Hi Ernesto,
Could you please try this mapping
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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 org.xml.sax.InputSource;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
import com.sap.aii.mapping.api.StreamTransformationException;
public class DOMParser2 extends AbstractTransformation {
public void execute(InputStream in, OutputStream out)
throws StreamTransformationException {
try
{
getTrace().addInfo("JAVA Mapping Iniciado");
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builderel=factory.newDocumentBuilder();
/*input document in form of XML*/
Document docIn=builderel.parse(in);
/*document after parsing*/
Document docOut=builderel.newDocument();
TransformerFactory tf=TransformerFactory.newInstance();
Transformer transform=tf.newTransformer();
Element root,child,child1;
Node rootIn;
Node sChild=null;
String xmlContent=null;
rootIn=docIn.getDocumentElement().getFirstChild();
if(rootIn.getNodeType()==Node.TEXT_NODE)
{
xmlContent=rootIn.getTextContent();
}
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlContent));
docIn = db.parse(is);
sChild=docIn.getElementsByTagName("ERRORES").item(0);
root=docOut.createElement("ns1:ZDM_RFC_REPLICA_CONVENIOS_PI.Response");
root.setAttribute("xmlns:ns1","urn:sap-com:document:sap:rfc:functions");
child1=docOut.createElement("E_RESULTADO");
child=(Element)docOut.importNode(sChild,true);
child1.appendChild(child);
docOut.appendChild(root);
root.appendChild(child1);
getTrace().addInfo("JAVA Mapping Finalizado");
transform.transform(new DOMSource(docOut), new StreamResult(out));
}
catch(Exception e)
{
getTrace().addInfo("Excepcion: " + e.getMessage());
e.printStackTrace();
}
}
@Override
public void transform(TransformationInput arg0, TransformationOutput arg1)
throws StreamTransformationException {
// TODO Auto-generated method stub
this.execute(arg0.getInputPayload().getInputStream(), arg1.getOutputPayload().getOutputStream());
}
}
Regards
Anupam
Hello Anupam, first, thank you for your cooperation and hope you have gotten the new year.
When we tested the code in SAP Netweaver Dev Studio, it works ok but then import the. jar in the Operation Mapping Test tab displays the following error:
"Unable to display tree view; Error when parsed an XML document(Premature end of file)"
Perform a debug your code facilitaste us, and the error mentioned, displayed on the line passing
53. docIn = db.parse (is);
any ideas to resolve the error?
Regards.
Hi Ernesto,
Please try this code. Hope the problem is resolved now.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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 org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
import com.sap.aii.mapping.api.StreamTransformationException;
public class DOMParser2 extends AbstractTransformation {
public void execute(InputStream in, OutputStream out)
throws StreamTransformationException {
try
{
getTrace().addInfo("JAVA Mapping Iniciado");
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builderel=factory.newDocumentBuilder();
Document docIn=builderel.parse(in);
Document docOut=builderel.newDocument();
TransformerFactory tf=TransformerFactory.newInstance();
Transformer transform=tf.newTransformer();
Element root,child,child1;
Node rootIn;
Node sChild=null;
String xmlContent=null;
rootIn=docIn.getDocumentElement().getFirstChild();
if(rootIn.getNodeType()==Node.TEXT_NODE)
{
xmlContent=rootIn.getTextContent();
}
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
docIn = db.parse(new InputSource(new StringReader( xmlContent ) ));
sChild=docIn.getElementsByTagName("ERRORES").item(0);
root=docOut.createElement("ns1:ZDM_RFC_REPLICA_CONVENIOS_PI.Response");
root.setAttribute("xmlns:ns1","urn:sap-com:document:sap:rfc:functions");
child1=docOut.createElement("E_RESULTADO");
child=(Element)docOut.importNode(sChild,true);
child1.appendChild(child);
docOut.appendChild(root);
root.appendChild(child1);
NodeList l=null;
l=docOut.getElementsByTagName("COD_RECHAZO");
int i=0,n=0;
if(l!=null)
{
n=l.getLength();
}
for(i=0;i<n;++i)
{
Node temp;
temp=l.item(i);
temp=docOut.renameNode(temp,"","CODIGO_RECHAZO");
}
docOut.setXmlStandalone(true);
getTrace().addInfo("JAVA Mapping Finalizado");
transform.transform(new DOMSource(docOut), new StreamResult(out));
}
catch(Exception e)
{
getTrace().addInfo("Excepcion: " + e.getMessage());
e.printStackTrace();
}
}
@Override
public void transform(TransformationInput arg0, TransformationOutput arg1)
throws StreamTransformationException {
// TODO Auto-generated method stub
this.execute(arg0.getInputPayload().getInputStream(), arg1.getOutputPayload().getOutputStream());
}
}
Regards
Anupam
Dear, the problem was solved. Given the structure of the RFC destination contains a table, a field named item included in the same denoting that it is a table. Therefore we had to adapt our xml output with that name. Attached is the code used.
Thank you all!
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
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 org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
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;
public class string_to_xml extends AbstractTransformation
{
@Override
public void transform(TransformationInput in, TransformationOutput out)
throws StreamTransformationException
{
String inData = "";
try
{
getTrace().addInfo("JAVA Mapping Iniciado");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document docIn = db.parse(in.getInputPayload().getInputStream());
NodeList nodos = docIn.getChildNodes();
if (nodos.item(0) != null)
{
String xmlEmbebido = nodos.item(0).getTextContent();
xmlEmbebido = xmlEmbebido.replaceAll("XML_OUTPUT","E_RESULTADO");
xmlEmbebido = xmlEmbebido.replaceAll("<ERROR>","<item>");
xmlEmbebido = xmlEmbebido.replaceAll("</ERROR>","</item>");
xmlEmbebido = xmlEmbebido.replaceAll("COD_RECHAZO","CODIGO_RECHAZO");
InputSource source = new InputSource(new StringReader(xmlEmbebido));
Document docOut = db.newDocument();
Document docTemp = db.parse(source);
Element root, child;
Node xmlOutput = null;
xmlOutput = docTemp.getElementsByTagName("E_RESULTADO").item(0);
child = (Element) docOut.importNode(xmlOutput, true);
root = docOut.createElement("ns1:ZDM_RFC_REPLICA_CONVENIOS_PI.Response");
root.setAttribute("xmlns:ns1","urn:sap-com:document:sap:rfc:functions");
// root = docOut.createElement("ns1:INT_66_DM_RESP_MT");
// root.setAttribute("xmlns:ns1","http://bancor.com/pi/gral/int_66_dm");
docOut.appendChild(root);
root.appendChild(child);
inData = getStringFromDocument(docOut);
}
} catch (Exception e)
{
getTrace().addInfo("Excepcion: " + e.getMessage());
}
try
{
out.getOutputPayload().getOutputStream().write(
inData.getBytes("UTF-8"));
getTrace().addInfo("JAVA Mapping Finalizado");
} catch (IOException e1)
{
getTrace().addInfo("IOException e1: " + e1.getMessage());
}
}
public String getStringFromDocument(Document doc)
{
try
{
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return writer.toString();
} catch (TransformerException ex)
{
getTrace().addInfo("TransformerException e1: " + ex.getMessage());
return null;
}
}
}
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.