简体   繁体   中英

Mule Dataweave creating CSV field as object in XML

I am looking to map a CSV file to XML but the output is creating an object for each field.

The expected result (used as the example in Transform Message output):

<?xml version="1.0"?>
<User>
    <UserId>ON1234</UserId>
    <UserStatus>client1234</UserStatus>
</User>

What I am getting is:

<?xml version='1.0' encoding='windows-1252'?>
<User>
  <UserId>
    <UserId></UserId>
    <UserId>ON1234</UserId>
    <UserId></UserId>
    <UserId>ON1235</UserId>
  </UserId>
</User>

When I add the mapping for UserStatus it shows this error:

java.util.NoSuchElementException: None.get

CSV file (used as the example in Transform Message input):

UserId,UserStatus
ON1234,active
ON1235,active

Mule flow XML

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

<mule xmlns:ftp="http://www.mulesoft.org/schema/mule/ftp" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:sftp="http://www.mulesoft.org/schema/mule/sftp" xmlns:batch="http://www.mulesoft.org/schema/mule/batch" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/sftp http://www.mulesoft.org/schema/mule/sftp/current/mule-sftp.xsd
http://www.mulesoft.org/schema/mule/batch http://www.mulesoft.org/schema/mule/batch/current/mule-batch.xsd
http://www.mulesoft.org/schema/mule/ee/ftp http://www.mulesoft.org/schema/mule/ee/ftp/current/mule-ftp-ee.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.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/ftp http://www.mulesoft.org/schema/mule/ftp/current/mule-ftp.xsd">
    <http:request-config name="HTTP_Request_Configuration" host="${host}" port="${userprocess.port}" doc:name="HTTP Request Configuration"></http:request-config>
    <sftp:connector name="SFTP" validateConnections="true" doc:name="SFTP"/>
    <flow name="userexperienceFlow" >
        <sftp:inbound-endpoint connector-ref="SFTP" host="localhost" port="2222" path="//input" user="${ftp.user}" password="${ftp.password}" responseTimeout="10000" doc:name="SFTP"/>
        <dw:transform-message metadata:id="59fade01-691b-41c6-a0b0-a9ff4b591d68" doc:name="Transform Message">
            <dw:input-payload doc:sample="list_csv_4.csv"/>
            <dw:set-payload><![CDATA[%dw 1.0
%output application/xml
---
{
    User: {
        UserId: payload.UserId,
        UserStatus: payload.UserStatus
    }
}]]></dw:set-payload>
        </dw:transform-message>
        <object-to-string-transformer doc:name="Object to String"/>  
        <logger level="INFO" doc:name="Logger"></logger>  
    </flow>
</mule>

Thanks

Your expected result is confusing, as your input is list of user and you are trying to create only one user. Try this

%dw 1.0
 %output application/xml
 ---
Users: {(payload map {
    User : {
        UserId: $.UserId,
        UserStatus: $.UserStatus
    }
})}

Which generates

<Users>
  <User>
    <UserId>ON1234</UserId>
    <UserStatus>active</UserStatus>
  </User>
  <User>
    <UserId>ON1235</UserId>
    <UserStatus>active</UserStatus>
  </User>
</Users>

Or for getting single user try this

%dw 1.0
 %output application/xml
 ---
(payload map {
    User : {
        UserId: $.UserId,
        UserStatus: $.UserStatus
    }
})[0]

Or this

%dw 1.0
%output application/xml
---
User : {
    UserId: payload[0].UserId,
    UserStatus: payload[0].UserStatus
}

where [0] returns first element with output as

<User>
  <UserId>ON1234</UserId>
  <UserStatus>active</UserStatus>
</User>

Hope this helps.

Hi Make use of below script

User:{
(payload map {
Users:{
($)
}
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM