简体   繁体   English

使用DataWeave 2.0将XML转换为CSV

[英]Convert xml to csv using dataweave 2.0

I have a person.xml, 我有一个person.xml,

<?xml version="1.0" encoding="UTF-8"?>
<person>
    <firstName>First Name</firstName>
    <lastName>Last Name</lastName>
</person

I have person.xsd, 我有person.xsd,

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="person">
<xs:complexType>
  <xs:sequence>
    <xs:element name="firstName" type="xs:string"/>
    <xs:element name="lastName" type="xs:string"/>
  </xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

I need to convert it to person.csv, 我需要将其转换为person.csv,

firstName,lastName
First Name,Last Name

I am using Anypoint Studio 7.3 on windows 10, I am using a Listener, Read (file) xml. 我在Windows 10上使用Anypoint Studio 7.3,我在使用侦听器,读取(文件)xml。

See Transform Message dw below, 请参阅下面的转换消息dw,

%dw 2.0
output application/csv separator=",", header=true
--- 
payload

I am getting the following message on executing the flow, 我在执行流程时收到以下消息,

"Cannot coerce Object { encoding: UTF-8, mediaType: text/xml; charset=UTF-8, mimeType: text/xml, raw: org.mule.weave.v2.el.SeekableCursorStream@2fdc554d } ({person: {firstName: "Shahjahan",lastName: "Ravjee"}} as Object {encoding: "U...) to Array, while writing CSV at payload." evaluating expression: "%dw 2.0
output application/csv separator=",", header=true
--- 
payload
    ".

The problem is, as the error message is stating, you're trying to convert an object into an array. 问题是,如错误消息所示,您正在尝试将对象转换为数组。 This is a problem because in order to make a CSV, you need an array of arrays, or an array of objects. 这是一个问题,因为要制作CSV,您需要一个数组数组或一个对象数组。

You can see if you're currently outputting an object or array by setting your output to application/dw . 通过将输出设置为application/dw可以查看当前是否正在输出对象或数组。

%dw 2.0
output application/dw
---
payload

This will give you the following, which as you can see is an object: 这将为您提供以下内容,如您所见,它是一个对象:

{
  person: {
    firstName: "First Name",
    lastName: "Last Name"
  }
}

CSV needs an array, so we'll need to do a little transformation to get it to this: CSV需要一个数组,因此我们需要做一些转换才能做到这一点:

[
  {
    firstName: "First Name",
    lastName: "Last Name"
  }
]

If you set the following script to output application/dw , you'll see that it outputs the above. 如果将以下脚本设置为output application/dw ,则会看到它输出了上面的脚本。 At this point, you can be sure you have the correct format needed to create a CSV (array of objects, in this case), so switch it back to output application/csv and you should be good to go: 在这一点上,您可以确定自己具有创建CSV(在这种情况下为对象数组)所需的正确格式,因此将其切换回output application/csv即可:

%dw 2.0
output application/csv
---
payload pluck($)

The following dw seems to have worked like a charm, 下列dw似乎起了作用,

  %dw 2.0
    output application/csv headerLineNumber = 0 , header = true
    ---
    [{
        firstName: payload.person.firstName,
        lastName: payload.person.lastName
    }
    ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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