Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain XML encoding type?

Tags:

dataweave

My current payload is (received from a POST)

<?xml version='1.0' encoding='ISO-8859-1'?>
<test>
</test>

I want to get the encoding value (ie ISO-8859-1)

What is the correct DataWeave expression to use ?

I already tested

var infos = payload.^mediaType splitBy "; "
var encoding = infos[1] splitBy  "="
---
media:
{ 
    mime: infos[0],
    encoding: encoding[1]
}

But it return me:

{
"mime": "application/xml",
"encoding": "UTF-8"
}

It seems payload.^mediaType is coming from my POST headers.

Here is the solution to the problem:

%dw 2.0
output application/java
---
((payload.^raw as String) scan /encoding='([A-z0-9-]+)'/)[0][1]

In the proposed solution the ^ symbol is missing.

like image 530
Sébastien Colas Avatar asked Jan 25 '23 19:01

Sébastien Colas


1 Answers

Sadly there is no elegant way to do this. The only way I thought is by using regex.

%dw 2.0
output application/java
---
((payload.^raw as String) scan /encoding='([A-z0-9-]+)'/)[0][1]

This is going to return the encoding.

like image 80
machaval Avatar answered Feb 11 '23 14:02

machaval