Mule Dataweave在XML中创建CSV字段作为对象

时间:2016-09-30 09:16:10

标签: xml csv mule anypoint-studio dataweave

我希望将CSV文件映射到XML,但输出是为每个字段创建一个对象。

预期结果(用作转换消息输出中的示例):

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

我得到的是:

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

当我为UserStatus添加映射时,它显示以下错误:

java.util.NoSuchElementException: None.get

CSV文件(用作转换消息输入中的示例):

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>

由于

2 个答案:

答案 0 :(得分:0)

您的预期结果令人困惑,因为您的输入是用户列表,而您尝试只创建一个用户。试试这个

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

生成

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

或者让单个用户试试这个

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

或者这个

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

其中[0]返回第一个元素,输出为

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

希望这有帮助。

答案 1 :(得分:0)

您好使用以下脚本

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