XML processing or parsing

 

XML processing or parsing in Dataweave become somewhat tricky as it depend on the input type of the XML

Two type of XML input request we can get

Include NameSpace

<?xml version = "1.0" encoding="UTF-8" standalone="yes"?>
<org:employees xmlns:org="http://www.mycompany.org/employees">
  <org:employee>
    <org:name age="22">
      <org:lastname>Kelly</org:lastname>
      <org:firstname>Grace</org:firstname>
    </org:name>
    <org:hiredate>October 15, 2005</org:hiredate>
    <org:projects>
      <org:project>
        <org:product>Printer</org:product>
      </org:project>
    </org:projects>
  </org:employee>
  <org:employee>
    <org:name age="32">
      <org:lastname>Grant</org:lastname>
      <org:firstname>Cary</org:firstname>
    </org:name>
    <org:hiredate>October 20, 2005</org:hiredate>
    <org:projects>
      <org:project>
        <org:product>Desktop</org:product>
      </org:project>
    </org:projects>
  </org:employee>
  <org:employee>
    <org:name age="42">
      <org:lastname>Gable</org:lastname>
      <org:firstname>Clark</org:firstname>
    </org:name>
    <org:hiredate>October 25, 2005</org:hiredate>
    <org:projects>
      <org:project>
        <org:product>Keyboard</org:product>
      </org:project>
    </org:projects>
  </org:employee>
</org:employees>

 

No Namespace

<?xml version = "1.0" encoding="UTF-8" standalone="yes"?>
<employees>
  <employee>
    <name age="22">
      <lastname>Kelly</lastname>
      <firstname>Grace</firstname>
    </name>
    <hiredate>October 15, 2005</hiredate>
    <projects>
      <project>
        <product>Printer</product>
      </project>
    </projects>
  </employee>
  <employee>
    <name age="32">
      <lastname>Grant</lastname>
      <firstname>Cary</firstname>
    </name>
    <hiredate>October 20, 2005</hiredate>
    <projects>
      <project>
        <product>Desktop</product>
      </project>
    </projects>
  </employee>
  <employee>
    <name age="42">
      <lastname>Gable</lastname>
      <firstname>Clark</firstname>
    </name>
    <hiredate>October 25, 2005</hiredate>
    <projects>
      <project>
        <product>Keyboard</product>
      </project>
    </projects>
  </employee>
</employees>

 

Processing of both somewhat get differ in dataweave

Let’s start with Namespace first

For the namespace XML input below are the things we have to take care

  • Namespace should be defined to target the xml element like ns org http://www.mycompany.org/employees
  • If we have to capture the reoccurring element then we have to put * like *org#employee
  • To get the attribute we have to use like $.org#name.@age

Dataweave

%dw 2.0
output application/json
ns org http://www.mycompany.org/employees 
---
payload.org#employees.*org#employee map {
  fullname : $.org#name.org#firstname ++ " " ++ $.org#name.org#lastname,
  age: $.org#name.@age,
  hiredate : $.org#hiredate,
  product : $.org#projects.org#project.org#product
}

Output

[
  {
    "fullname": "Grace Kelly",
    "age": "22",
    "hiredate": "October 15, 2005",
    "product": "Printer"
  },
  {
    "fullname": "Cary Grant",
    "age": "32",
    "hiredate": "October 20, 2005",
    "product": "Desktop"
  },
  {
    "fullname": "Clark Gable",
    "age": "42",
    "hiredate": "October 25, 2005",
    "product": "Keyboard"
  }
]

 

With filter criteria

%dw 2.0
output application/json
ns org http://www.mycompany.org/employees 
---
payload.org#employees.*org#employee filter($.org#name.@age > 30) map {
  fullname : $.org#name.org#firstname ++ " " ++ $.org#name.org#lastname,
  age: $.org#name.@age,
  hiredate : $.org#hiredate,
  product : $.org#projects.org#project.org#product
}

Output

[
  {
    "fullname": "Cary Grant",
    "age": "32",
    "hiredate": "October 20, 2005",
    "product": "Desktop"
  },
  {
    "fullname": "Clark Gable",
    "age": "42",
    "hiredate": "October 25, 2005",
    "product": "Keyboard"
  }
]

 

Let process input XML having no namespace

We don’t have to include namespace while targeting the XML element

Dataweave

%dw 2.0
output application/json
---
payload.employees.*employee map {
  fullname : $.name.firstname ++ " " ++ $.name.lastname,
  age: $.name.@age,
  hiredate : $.hiredate,
  product : $.projects.project.product
}

Output

[
  {
    "fullname": "Grace Kelly",
    "age": "22",
    "hiredate": "October 15, 2005",
    "product": "Printer"
  },
  {
    "fullname": "Cary Grant",
    "age": "32",
    "hiredate": "October 20, 2005",
    "product": "Desktop"
  },
  {
    "fullname": "Clark Gable",
    "age": "42",
    "hiredate": "October 25, 2005",
    "product": "Keyboard"
  }
]

 

With filter criteria

%dw 2.0
output application/json
ns org http://www.mycompany.org/employees 
---
payload.employees.*employee filter($.name.@age > 30) map {
  fullname : $.name.firstname ++ " " ++ $.name.lastname,
  age: $.name.@age,
  hiredate : $.hiredate,
  product : $.projects.project.product
}

Output

[
  {
    "fullname": "Cary Grant",
    "age": "32",
    "hiredate": "October 20, 2005",
    "product": "Desktop"
  },
  {
    "fullname": "Clark Gable",
    "age": "42",
    "hiredate": "October 25, 2005",
    "product": "Keyboard"
  }
]

more detail on XML namespace 

  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Venkat
Venkat
2 years ago

I have tested the scenarios and processing of both is not different. We can definitely use the same dataweave for no namespace XML in both the use cases.

Last edited 2 years ago by Venkat
Radhekrishna
Radhekrishna
2 years ago

Hi,
someone please help me to solve the below issue

I need to fetch the data from SQL DB and the data should be in XML format in postman

RUKMINI
RUKMINI
2 years ago

any one help on get api in mulesoft output in postman xml format

Last edited 2 years ago by RUKMINI
RUKMINI
RUKMINI
2 years ago

Post Api In Mulesoft

Last edited 2 years ago by RUKMINI