无法在 Mule4 的变量中设置 XML 负载

Unable to set XML payload in variable in Mule4

我是 Mule4 的新手。我的输入消息是 XML 未在变量中设置。

我也尝试过使用不同的 mime 类型,但没有用。但是,当我以 JSON 格式而不是 XML.

格式发送输入时,它确实有效

我正在通过邮递员使用以下 XML

<Weather>
    <City>London,uk</City>
    <appid>b6907d289e10d714a6e88b30761fae22</appid>
    <CIF>CIF20257</CIF>
</Weather>

我讨论的代码的配置 XML 是

<set-variable value="#[payload.Weather.City]" doc:name="Set Variable" doc:id="b98b3ec8-c1f7-436d-9bcf-49eb0ca8a033" variableName="test" mimeType="application/xml"/>

显示的错误是

"javax.xml.stream.XMLStreamException - Trying to output non-whitespace characters outside main element tree (in prolog or epilog), while writing Xml. Trace: at main (Unknown)" evaluating expression: "payload.Weather.City".

设置变量有两种方法。

  1. 使用设置变量组件
  2. 正在使用数据编织转换。

我使用了第二种方法,我可以看到我能够设置变量

这是小示例应用程序的完整代码:

    <?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http"
    xmlns="http://www.mulesoft.org/schema/mule/core"
    xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
    <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="23645d25-1194-4fcd-ae19-ffae9b9388f8" basePath="/play" >
        <http:listener-connection host="localhost" port="8081" />
    </http:listener-config>
    <flow name="z_playFlow" doc:id="2ae13c16-4e1e-4203-96c3-9d372ce41c63" >
        <http:listener doc:name="Listener" doc:id="9fa851c0-a05b-46e1-9ba4-f2433c80d67a" config-ref="HTTP_Listener_config" path="/setxml"/>
        <set-payload value="&lt;Weather&gt;
    &lt;City&gt;London,uk&lt;/City&gt;
    &lt;appid&gt;b6907d289e10d714a6e88b30761fae22&lt;/appid&gt;
    &lt;CIF&gt;CIF20257&lt;/CIF&gt;
&lt;/Weather&gt;" doc:name="Set Payload" doc:id="7d122f45-6025-4fb8-a7d4-e1ec0873f40b" mimeType="application/xml"/>
        <ee:transform doc:name="Transform Message" doc:id="af6467e5-7177-403c-b9c0-62fb816b8f60" >
            <ee:message >
            </ee:message>
            <ee:variables >
                <ee:set-variable variableName="var" ><![CDATA[%dw 2.0
output application/xml
---

city: payload.Weather.City]]></ee:set-variable>
            </ee:variables>
        </ee:transform>
        <logger level="INFO" doc:name="Logger" doc:id="8cbdcf0f-8b3e-4645-9475-887b9628bc05" message="#[payload]"/>
    </flow>
</mule>

如果您对如何通过 "transform message" 组件定义变量有疑问,请告诉我,我可以向您演示。


在转换消息中定义一个变量

  1. 当您拉入转换消息组件时,默认输出类型是该组件的有效负载。像这样

  2. 单击编辑当前目标(笔)选项,这将打开一个 selections 对话框,并在输出下拉列表下 select 变量并提供一个变量名称:

我 运行 在从 Mule 3 思维模式转移到 Mule 4 时遇到了这个问题。你的错误原因是你的变量类型是 XML,但你试图写non-xml到它。

payload.Weather.City 的输出是无效的字符串 London,uk XML。有几个选项可以解决这个问题。

  1. 输出有效XML到变量
<set-variable value="#[City: payload.Weather.City]" doc:name="Set Variable" variableName="test" />

这会将变量的值设置为<City>London,uk</City,这是有效的XML

  1. 更改变量类型

如果只是希望将StringLondon,uk存储起来以备后用,那么可以显式设置set变量组件的输出类型为java.

<set-variable value="#[output application/java --- payload.Weather.City]" doc:name="Set Variable" variableName="test" />