Hi all.

In this blog i’ll show my way to use 1-1 MM in 1-n OM.

What is it for?

In my case i had OM 1-1 with MM 1-1. But in one of these fine days it became necessary to change OM to 1-n.

And i change it. And realize, that it does not work with 1-1 MM.

The reason is headers of multi-mapping messages <ns0:Messages xmlns:ns0=”http://sap.com/xi/XI/SplitAndMerge“><ns0:Message1></ns0:Message1</ns0:Messages>

I had two opportunities:

1. Change my MM to 1-n.

2. Clear headers from message before MM and add them after MM.

I have some reasons why i can’t change my MM to 1-n. Maybe just because of laziness.

So, I wrote two Java Mappings. One clear headers, and second inserts them.

And in OM it looks like this:

—-Mapping programms—————–

Java Class : RemoveMultiNodes.java

MM: My1to1Mapping

JavaClass: InsertMultimNodes.java

—————————————–

But you must be carefull. This works in Runtime, but if you want to test it through ESB, you need to test OM from second step.

Codes:

public class RemoveMultiNodes extends
AbstractTransformation {

     @Override
     public void transform(TransformationInput in, TransformationOutput out)
               throws StreamTransformationException {
          // TODO Auto-generated method stub
         
          AbstractTrace trace = getTrace();
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
          DocumentBuilder db;
         
          try {
               db = dbf.newDocumentBuilder();
               Document xml_in = db.parse(in.getInputPayload().getInputStream());
               StringWriter stringWriter = new StringWriter();
               Transformer transformer = TransformerFactory.newInstance().newTransformer();
               transformer.setOutputProperty(OutputKeys.INDENT, "yes");
               transformer.transform(new DOMSource(xml_in), new StreamResult(stringWriter));

             String InboundXml = stringWriter.toString(); //This is string data of xml file
             InboundXml = InboundXml.replace("<sxi:Messages xmlns:sxi=\"http://sap.com/xi/XI/SplitAndMerge\">", "");
             InboundXml = InboundXml.replace("<sxi:Message1>","");
             InboundXml = InboundXml.replace("</sxi:Message1>","");
             InboundXml = InboundXml.replace("</sxi:Messages>","");
            
             out.getOutputPayload().getOutputStream().write(InboundXml.getBytes("UTF-8"));
          }
               catch (ParserConfigurationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               } catch (SAXException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               } catch (TransformerConfigurationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               } catch (TransformerFactoryConfigurationError e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               } catch (TransformerException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               }         
     }
}
Or short xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="/">
        <xsl:copy-of select="/*/*/*"/>
    </xsl:template>
</xsl:stylesheet>
------------------------------------------------------------------------------
public class InsertMultimNodes extends
          AbstractTransformation {
    
     @Override
     public void transform(TransformationInput in, TransformationOutput out)
               throws StreamTransformationException {
          // TODO Auto-generated method stub
          AbstractTrace trace = getTrace();
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
          DocumentBuilder db;
          try {
               db = dbf.newDocumentBuilder();
               Document xml_in = db.parse(in.getInputPayload().getInputStream());
                               
               Transformer transformer = TransformerFactory.newInstance().newTransformer();
               transformer.setOutputProperty(OutputKeys.INDENT, "yes");

               StreamResult result = new StreamResult(new StringWriter());
              
              
               DOMSource source = new DOMSource(node);
               transformer.transform(source, result);

               String xmlString = result.getWriter().toString();
              
               StringWriter stringWriter = new StringWriter();

             transformer.transform(new DOMSource(xml_in), new StreamResult(stringWriter));

            String InboundXml = stringWriter.toString(); //This is string data of xml file
              
            InboundXml =  InboundXml.replace("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>", "");
            
             InboundXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
             "<ns0:Messages xmlns:ns0=\"http://sap.com/xi/XI/SplitAndMerge\"><ns0:Message1>"
             + InboundXml +
             "</ns0:Message1></ns0:Messages>";
             out.getOutputPayload().getOutputStream().write(InboundXml.getBytes("UTF-8"));
              
          } catch (ParserConfigurationException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          } catch (SAXException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          } catch (TransformerConfigurationException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          } catch (TransformerFactoryConfigurationError e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          } catch (TransformerException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }                 
     }
}
Or xslt: 
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="/">
        <ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
            <ns0:Message1>
                <xsl:copy-of select="."/>
            </ns0:Message1>
        </ns0:Messages>
    </xsl:template>
</xsl:stylesheet>

Have fun 🙂

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply