You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Mule中无法转换UTF-16格式XML的问题求助

问题解决:Mule中UTF-16 XML转换编码不匹配错误

问题场景

收到声明为UTF-16的XML输入:

<?xml version="1.0" encoding="utf-16"?>
<Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Body>
        <DOC_POST>
            <ACCOUNT>
                <Item>
                    <PLANT>1000</PLANT>
                </Item>
           </ACCOUNT>
        </DOC_POST>
    </Body>
</Envelope>

使用如下DataWeave转换时:

%dw 2.0
output application/json
---
payload

在Mule运行时(4.3.0,Anypoint Studio 7.10.0)出现错误:

Declared encoding 'UTF-16' uses 2 bytes per character; but physical encoding appeared to use 1; cannot decode
at [row,col {unknown-source}]: [1,40], while reading payload as Xml.
[row,col]: [1,40]" evaluating expression: "%dw 2.0
output application/json

payload"

但相同内容在DataWeave playground中转换正常。

错误原因

核心问题是XML声明的编码与实际字节流的编码不匹配:XML头部声明为UTF-16(双字节编码),但Mule读取到的实际字节流是单字节编码(如UTF-8),导致解码失败。

DataWeave playground能正常处理是因为它以文本形式接收输入,会自动适配编码;而Mule运行时直接处理原始字节流,对编码的一致性要求更严格。

解决方案

1. 确认输入的实际编码

先排查数据源的真实编码:

  • 文件输入:用文本编辑器(如Notepad++)查看文件实际编码;
  • 网络请求:检查请求头Content-Type是否正确携带charset=utf-16;
  • 若数据源实际是UTF-8,直接修改XML头部的encoding属性为utf-8。

2. 在Mule中显式指定编码

如果输入确实是UTF-16,需明确指定编码确保字节流被正确解码:

  • 连接器配置编码:在HTTP Listener、File Connector等组件的编码配置项中设置为UTF-16;
  • DataWeave显式读取:使用read函数强制指定编码,示例:
%dw 2.0
output application/json
---
read(payload, "application/xml", {"encoding": "UTF-16"})

3. 处理UTF-16的BOM问题

UTF-16分大端(UTF-16BE)和小端(UTF-16LE),若无BOM标识Mule可能无法自动识别,尝试指定具体变体:

read(payload, "application/xml", {"encoding": "UTF-16LE"})
// 或
read(payload, "application/xml", {"encoding": "UTF-16BE"})

4. 排查中间环节的编码转换

检查代理、网关等中间传输环节是否擅自修改编码,确保字节流从数据源到Mule的过程中未被转码。


内容的提问来源于stack exchange,提问作者shyama moneymohan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 04:22:37