Skip to Content
Technical Articles
Author's profile photo Anupam Ghosh

Use of return as XML feature and DOM parser in graphical mapping

Problem statement: 

Please consider below  payload structures

Source%20data%20structure

Source data structure

 

Target%20data%20structure

Target data structure

Suppose we need to create 4 Strc elements in target group with values from the field S1,S2,S3,S4 in source.

Solution: 

The problem can be solved easily by use of duplicate subtree 3 times option in target side and direct mapping of source fields to target fields.

However I wanted to explore the use of “Return as XML” feature and use of DOM parser in creation of target payload. So this is the solution

Message%20Mapping

Message Mapping

When connecting item_source node to user defined function createTag used the feature “return as XML”.

If you need to understand or learn  in depth  “return as XML” mapping feature available in PO 7.5 server, please kindly refer to this beautiful blog.

Now here is the UDF. I have explained each statement of the code using comments. However if you still feel some more explanation of the code is necessary please mention the same in comments will definitely include those in the depicted code.

 

public void createTag(String[] var1, ResultList result, Container container) throws StreamTransformationException{
try
{
//Parser that produces DOM object trees from XML content
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         
        //API to obtain DOM Document instance
        DocumentBuilder builder = null;
      
            //Create DocumentBuilder with default configuration
        builder = factory.newDocumentBuilder();
             
            //Parse the content to Document object
        Document doc = builder.parse(new InputSource(new StringReader(var1[0])));
       //get all children nodes of the node item_source
        NodeList childNodes=doc.getDocumentElement().getChildNodes();
      // Now selectively move the contents of  nodes S1,S2,S3 and S4 to target node 
        for(int i=0;i<childNodes.getLength();++i)
        {
        		if(childNodes.item(i).getNodeType()==Node.ELEMENT_NODE)
         	{
        				 result.addValue(childNodes.item(i).getTextContent());
         	}
        }
		
}
catch(Exception e)
{
		throw new StreamTransformationException(e.getMessage());
}
}

Here are the import statements necessary for running this UDF

 

Import%20statements%20for%20the%20UDF

Import statements for the UDF

 

Output:

Test%20results%20of%20the%20mapping

Test results of the mapping

 

As we can see the fields S1,S2,S3, S4 were never mapped to any field in target however with use of “Return as XML”  along with use of DOM parser they have been mapped to the target. With these two tools most complex mapping issue can be resolved.

Assigned Tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Subin S
      Subin S

      Useful post. Where can i learn java for understanding udf, I don't think learning java in general will help...could you please guide me on this ?

      Author's profile photo Anupam Ghosh
      Anupam Ghosh
      Blog Post Author

      Hi Subin S,

      Thank you for going through this blog post. This kind of UDF should only be used when all standard mapping functions provided by SAP fails to meet the mapping complexity.  Learning java is important to know basics of programming. Thus you should know core java. Second to write this kind  of UDF you need to understand how to manipulate XML with java with help of DOM or SAX parser. Thirdly before writing the UDF you need to understand what is context and how in UDF you can manipulate the contexts.  Here are few useful links which gives you good knowledge on the topics

      1. https://www.tutorialspoint.com/java_xml/java_dom_parse_document.htm  ---- DOM parser
      2. https://www.tutorialspoint.com/java/index.htm     ----- core java fundamentals

      3. https://blogs.sap.com/2008/12/08/context-and-queue-in-message-mapping-udf/ --- difference             between context and qued UDF.

      Hope these links help. Let me know in case you face any difficulty in writing UDF.

       

      Regards

      Anupam