Simbu
Simbu

Reputation: 796

How to access the XML which comes out of Dataweave

I try to access the elements under xml which is comes out of DataWeave. It gives returns me null values.

DataWeave Script is

%dw 1.0
%namespace ns0 urn:abc:dbc:Components
%output text/xml
---
ItemFee:{
    product_id:flowVars."Dept_id",
    TotalFees: sum payload.ns0#ItemResponse.ns0#Fees.*ns0#Fee.ns0#Fee
}

Immediate after to this dataweave i have logger node with below message.

#[message.payload.ItemFee.TotalFees]

I am getting error saying

Execution of the expression "message.payload.ItemFee.TotalFees" failed. (org.mule.api.expression.ExpressionRuntimeException). Message payload is of type: WeaveMessageProcessor$WeaveOutputHandler

I like to say one more point here. When i give below text in the logger immediate after 'Transform Message'. The message printed in the console without issue. But i could not access the elements in the xml message.#[message.payloadAs(java.lang.String)]

Upvotes: 1

Views: 1511

Answers (2)

Simbu
Simbu

Reputation: 796

Thanks Ryan. Your answer helped me. I have entered below in my logger. Then i am able to fetch the items.

#[xpath3('//ItemFee/TotalFees')]

Earlier i tried these stuff's i am not sure why it not worked earlier. Might be i have overlooked the issue. :)

Upvotes: 0

Ryan Carter
Ryan Carter

Reputation: 11606

That MEL syntax only works with Java objects. As the ouput is XML, you will have to use the xpath3 MEL function: https://docs.mulesoft.com/mule-user-guide/v/3.7/xpath#the-xpath3-function

Something like:

#[xpath3('//ItemFee/TotalFees').text]

Upvotes: 3

Related Questions