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#[email protected]

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#[email protected],
  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#[email protected] > 30) map {
  fullname : $.org#name.org#firstname ++ " " ++ $.org#name.org#lastname,
  age: $.org#[email protected],
  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: [email protected],
  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([email protected] > 30) map {
  fullname : $.name.firstname ++ " " ++ $.name.lastname,
  age: [email protected],
  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
0 Comments
Inline Feedbacks
View all comments