如何在DataWeave中提取嵌套数组值frm XML并转换为CSV

时间:2019-01-29 21:39:10

标签: anypoint-studio dataweave mulesoft

我正在从数据库中获取此值

<row>
 <file_data>One</file_data>
 <time_inserted>2019-01-30T10:29:20.543</time_inserted>
</row>
<row>
 <file_data>two</file_data>
 <time_inserted>2019-01-30T10:29:20.547</time_inserted>
</row>
<row>
 <file_data>three</file_data>
 <time_inserted>2019-01-30T10:29:20.550</time_inserted>
</row>
<row>
 <file_data>four</file_data>
 <time_inserted>2019-01-30T10:29:20.550</time_inserted>
</row>
<row>
 <file_data>five</file_data>
 <time_inserted>2019-01-30T10:29:20.553</time_inserted>
</row>

我想像这样将其输出为CSV

one
two
three
four
five

任何帮助表示赞赏

谢谢

1 个答案:

答案 0 :(得分:0)

我设法解决了我的问题:

首先,我在逻辑中添加了一个步骤,以“行”元素包围数据,使其最终看起来像这样:

<rows>
    <row>
        <file_data>One</file_data>
        <time_inserted>2019-01-30T12:34:00.277</time_inserted>
    </row>
    <row>
        <file_data>two</file_data>
        <time_inserted>2019-01-30T12:34:00.277</time_inserted>
    </row>
    <row>
        <file_data>three</file_data>
        <time_inserted>2019-01-30T12:34:00.280</time_inserted>
    </row>
    <row>
        <file_data>four</file_data>
        <time_inserted>2019-01-30T12:34:00.280</time_inserted>
    </row>
    <row>
        <file_data>five</file_data>
        <time_inserted>2019-01-30T12:34:00.283</time_inserted>
    </row>
</rows>  

然后我编写此代码以获取所需的列表输出:

 def responses = new XmlSlurper().parseText(xmlData)
 def payload = ""

 responses.row.findAll { p ->
  p.file_data
 }
 .each { p ->
 payload = payload + "${p.file_data}" + "\r\n"
 }               

print payload

这将输出:

 one
 two
 three
 four
 five