Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
anupam_ghosh2
Active Contributor
0 Kudos
Problem statement: 

Please consider below  payload structures


Source data structure


 


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 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 statements for the UDF


 

Output:


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.
2 Comments
Labels in this area