cancel
Showing results for 
Search instead for 
Did you mean: 

string to XML Java mapping

ernesto_cruz
Participant
0 Kudos

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.

Accepted Solutions (1)

Accepted Solutions (1)

anupam_ghosh2
Active Contributor
0 Kudos

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

ernesto_cruz
Participant
0 Kudos

Hi Anupam, today I return to the work.

With respect the files, I come back post the file source, because the mapping that I need, is from String  to XML, file1ws -> file2rfc.

I revised and the file1ws is incorrect. I reloaded the file1ws.

Thanks a lot for your cooperation.

baskar_gopalakrishnan2
Active Contributor
0 Kudos

Just a  thought.  The error message shows the parser error is due to xml syntax problem. Please make sure the closing and beginning tags match each other in your mapping output.

ernesto_cruz
Participant
0 Kudos

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.

Former Member
0 Kudos

Hi Ernesto,

I assume you've created the proper Message Type for the response, haven't you?

cheers,

Edu

ernesto_cruz
Participant
0 Kudos

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.

Former Member
0 Kudos

Hi Ernesto,

I'm not very good a java mappings, but if you've got some graphical mapping that does work and insert those tags, you could locate that mapping jar, decompile it and see how it's done there. Or wait for the experts to shed some light here.

Cheers,

Edu

ernesto_cruz
Participant
0 Kudos

We keep trying to Java mapping, any help please.

Regards.

anupam_ghosh2
Active Contributor
0 Kudos

Wish all members of this amazing , wonderful and helpful forum a very happy new year.

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

ernesto_cruz
Participant
0 Kudos

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.

anupam_ghosh2
Active Contributor
0 Kudos

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

ernesto_cruz
Participant
0 Kudos

Hi Anupam, now the code works fine. But the overall testing, that is, the test launch from the RFC, we obtain a system failure response.

And from the RW, in the sender channel throws the next screenshot.

If necessary, we will create a new post. Thank you very much!

ernesto_cruz
Participant
0 Kudos

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;
  }

}
}

Answers (0)