0

我有一个应用程序/Java ArrayList。它包含 String 格式的 XML 元素。我想使用 Dataweave 2 添加到主/根节点:

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

但它返回:

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

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

4

2 回答 2

3

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>
于 2019-08-15T11:27:39.660 回答
0

你能试试这个吗

%dw 2.0
output application/xml
---
shops: read(vars.shop, "application/xml")
于 2019-08-14T17:51:23.317 回答