user15592843
user15592843

Reputation: 15

How to traverse xml file in Mulesoft

In my input payload I will get value Username=erepair, Password=erepairtest and InterfaceName=ABC. I need to check all these value exist under the particular partner node in a partner.xml file under scr/main/resource.

Basically the need is to check if the Interface "ABC" is authorized for erepair/erepairtest

    <PartnerMain>
        <Partner>
            <userName>erepair</userName>
            <userPassword>erepairtest</userPassword>
            <AuthorizedInterface>
                <name>ABC</name>
            </AuthorizedInterface>
            <AuthorizedInterface>
                <name>EFG</name>
            </AuthorizedInterface>
        </Partner>
        <Partner>
            <userName>pair</userName>
            <userPassword>pairtest</userPassword>
            <AuthorizedInterface>
                <name>ABC</name>
            </AuthorizedInterface>
            <AuthorizedInterface>
                <name>EFG</name>
            </AuthorizedInterface>
        </Partner>
    </PartnerMain>  

Upvotes: 0

Views: 342

Answers (1)

Salim Khan
Salim Khan

Reputation: 4303

Assuming you are going to use Mule 4.x runtime and thus DataWeave 2.0

You can apply a filter to the incoming input payload and check for the conditions to match.

For e.g. Lets say you have the variables containing the userName, password and InterfaceName ( since i am using playground and i wanted to show you a quick solution to the problem at hand). Lets say your payload is the partner.xml file (since you would be implementing this in a Mule flow, you would use a file:read operation and refer to it in the Dataweave Transform Message component.).

The script to check the fields against the payload would look something like this:

Script

%dw 2.0
output application/json
var userName="erepair"
var password="erepairtest"
var interfaceName="ABC"
---
(payload.PartnerMain.*Partner filter (($.userName == userName) and ($.userPassword == password) and ($.AuthorizedInterface.*name contains interfaceName)))[0]

Input

<PartnerMain>
        <Partner>
            <userName>erepair</userName>
            <userPassword>erepairtest</userPassword>
            <AuthorizedInterface>
                <name>ABC</name>
            </AuthorizedInterface>
            <AuthorizedInterface>
                <name>EFG</name>
            </AuthorizedInterface>
        </Partner>
        <Partner>
            <userName>pair</userName>
            <userPassword>pairtest</userPassword>
            <AuthorizedInterface>
                <name>ABC</name>
            </AuthorizedInterface>
            <AuthorizedInterface>
                <name>EFG</name>
            </AuthorizedInterface>
        </Partner>
    </PartnerMain>  

Output

{
  "userName": "erepair",
  "userPassword": "erepairtest",
  "AuthorizedInterface": {
    "name": "ABC"
  },
  "AuthorizedInterface": {
    "name": "EFG"
  }
}

Upvotes: 1

Related Questions