Dataweave - Trying to write END_DOCUMENT when document has no root

3.3k views Asked by At

I have an Application/Java ArrayList. That contains XML elements in String format. I want to add to a main/root node with Dataweave 2:

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

But it returns:

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

How can I solved that? I tried with application/java but still failing, the problem is into the write() method trying to transform the array to XML.

2

There are 2 answers

0
machaval On BEST ANSWER

What does the vars.shop has? Most commonly for xml building you need to use the dynamic object feature.

 %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     
        })
    }
}

This script outputs

<?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>
1
Srinivasan Raghunathan On

Could you please try this

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