Dataweave - 当文档没有根目录时尝试写入 END_DOCUMENT

Dataweave - Trying to write END_DOCUMENT when document has no root

我有一个 Application/Java ArrayList。包含字符串格式的 XML 个元素。 我想使用 Dataweave 2 添加到 main/root 节点:

%dw 2.0
output application/xml
---
shops: write(vars.shop, "application/xml")

但是 returns:

Caused by:
javax.xml.stream.XMLStreamException: Trying to write END_DOCUMENT when document has no root (ie. trying to output empty document)

我该如何解决?我尝试使用 application/java 但仍然失败,问题在于试图将数组转换为 XML.

的 write() 方法

你能试试这个吗

%dw 2.0
output application/xml
---
shops: read(vars.shop, "application/xml")

vars.shop有什么?最常见的是 xml 建筑,您需要使用动态对象功能。

 %dw 2.0
output application/xml
ns ns0 http://example/catalog/2002-13-23
var shops = ["data example","data example2","data example123","data example345","data example56"]
---
{
    ns0#shops @("shops-id": "demo"): {
        (shops map (shopName) -> {
            ns0#shop: shopName     
        })
    }
}

这个脚本输出

<?xml version='1.0' encoding='UTF-8'?>
<ns0:shops xmlns:ns0="http://example/catalog/2002-13-23" shops-id="demo">
  <ns0:shop>data example</ns0:shop>
  <ns0:shop>data example2</ns0:shop>
  <ns0:shop>data example123</ns0:shop>
  <ns0:shop>data example345</ns0:shop>
  <ns0:shop>data example56</ns0:shop>
</ns0:shops>