Skip to Content
Technical Articles

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.

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