Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

DataWeave 2.0 – Output an XML with multiple elements

With DataWeave 2.0 I can output a simple XML – the following code:

%dw 2.0
output application/xml

---
a:
b:
c: "content"

Would output:

<?xml version='1.0' encoding='UTF-8'?>
<a>
  <b>
    <c>content</c>
  </b>
</a>

But what if I want multiple elements under the b tag, such as:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?xml version='1.0' encoding='UTF-8'?>
<a>
  <b>
    <c>content</c>
    <d>dontent</d>
  </b>
</a>

I tried to use:

%dw 2.0
output application/xml

---
a:
b:
c: "content",
d: "dontent"

And I got the following error:

Invalid input ‘,’, expected ???

I also tried to just add d without the comma after c, but it didn’t work, either.

How can it be achieved?

I couldn’t find anything like it in the documentation or in the tutorial. Is there a way to get the XML I want without converting from JSON or any other type?

>Solution :

You can try this with ++ concantentation

%dw 2.0
output application/xml
---
a:
b:((c: "content")++("d": "dontent"))

Output

<?xml version='1.0' encoding='UTF-8'?>
<a>
  <b>
    <c>content</c>
    <d>dontent</d>
  </b>
</a>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading