cancel
Showing results for 
Search instead for 
Did you mean: 

Selecting multiple values in content modifiers

moosa
Explorer

Hi Experts,

I am trying to form the "Exchange Property" in Content Modifier which selects the values of a particular field in multiple nodes by using XPATH as type. But when I get the value of this property in groovy script or in another Content Modifier, I only get the first node value and the rest of the values in the successive nodes are ignored.

Input XML:

<?xml version="1.0" encoding="UTF-8"?> <User> <Records> <ID>1</ID> </Records> <Records> <ID>2</ID> </Records> <Records> <ID>3</ID> </Records> <Records> <ID>4</ID> </Records> </User>

Sample XPATH used in first Content Modifier to form the Exchange Property "ID" is :

p2:User/Records/ID/

Note: This XPATH is automatically selected in the Content Modifier.

I am trying to access the Exchange Property as below:

Groovy Script : message.getProperty("ID");

Content Modifier : ${property.ID}

Please let me know your suggestions

Regards,

Ahmed.

Accepted Solutions (1)

Accepted Solutions (1)

MortenWittrock
Active Contributor

Hi Ahmed

The data type you assign your property is important in this case. If you want to capture multiple nodes in the property, try org.w3c.dom.NodeList.

You can retrieve this object in your script, and use it to process the nodes.

Keep in mind, that you could also parse the XML directly in the script. But if you have a complicated XPath expression, it still might make more sense to create a property based on the XPath expression, and then process the resulting nodes in a script.

Regards,

Morten

former_member586532
Discoverer
0 Kudos

Hi Morten,

Thanks for the reply.

How can I access this Nodelist parameter in script. When I try to access it in groovy script and print it in log file attachment, system is printing "NULL". Can you please share the groovy code snippet to access this Exchange Property parameter and parse it.

Regards,

Ahmed.

MortenWittrock
Active Contributor

Hi Ahmed

The NodeList interface only has two methods, documented in the link I provided. Here's an example of extracting the values of the ID elements and adding them to the message body, separated by commas:

import com.sap.gateway.ip.core.customdev.util.Message
import org.w3c.dom.NodeList

def Message processData(Message message) {
    def nodes = message.getProperty("ID") as NodeList
    def ids = []
    for (i = 0; i < nodes.getLength(); i++) {
        ids.add(nodes.item(i).getTextContent())
    }
    message.setBody(ids.join(','))
    return message
}

Regards,

Morten

former_member586532
Discoverer
0 Kudos

That's Perfect Morten. Thanks for your instant support.

I am able to fetch the nodelist values now but by inserting a small logic in addition to your solution.

message.setBody(ids.join(','))

The above line in your code is causing the issue as the run-time engine is expecting the values in XML tags and we are populating the string values directly. Please find below the error:

Outbound processing in endpoint at /CreateUpdateUsers failed with message "Fault:Could not generate the XML stream caused by: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '1' (code 49) in prolog; expected '<' at [row,col {unknown-source}]: [1,1].", caused by "WstxUnexpectedCharException:Unexpected character '1' (code 49) in prolog; expected '<' at [row,col {unknown-source}]: [1,1]"

Note: The value starts with "1" and hence we see "1" in the above error.

Thanks again for your support.

Regards,

Ahmed.

MortenWittrock
Active Contributor
0 Kudos

Hi Ahmed

The script I posted was just an example of how to work with the NodeList interface. It will obviously not fit directly into the integration flow you are working on.

I feel the question, as originally stated, has been answered correctly. If you run into further issues while implementing your iflow, you are of course welcome to post new questions, that are specific to those issues.

Regards,

Morten

moosa
Explorer

That's correct Morten.

Thanks and Regards,

Ahmed.

Answers (0)