Search code examples
mulemule-studioanypoint-studio

Unable to set XML payload in variable in Mule4


I'm a newbie on Mule4. My input message is XML which isn't being set in variable.

I've tried it with different mime types as well, but doesn't work. It does work however when I'm sending the input in JSON format instead of XML.

I'm using the following XML via postman

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

And Configuration XML of my code in discussion is

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

Error being displayed is

"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".


Solution

  • There are two ways you can do a set Variable.

    1. using the set variable component
    2. is using a dataweave transformation.

    I have used the second approach and i can see that i am able to set the variable

    enter image description here

    here is the complete code for the small sample application :

        <?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>
    

    if you have question on how to define a variable via "transform message" component let me know and i can demonstrate that to you.


    defining a variable within transform message

    1. when you pull in a transform message component the default output type is payload for that. like this enter image description here

    2. click on the edit current target (pen) option which will open an selections dialog and under the output dropdown select Variable and supply a variable name:

      enter image description here