xml to List conversion in mule esb using data weave component

夙愿已清 提交于 2019-12-14 03:17:43

问题


I have the following xml as my input

InputXML:

<Orders>
<Order>
  <sample id="1"></sample>
  <sample id="2"></sample>
  .
  .
  .
  .    
 </Order>
<Order>
  <sample id="1"></sample>
  <sample id="2"></sample>
  .
  .
  .
  .    
 </Order>
<Order>
   <sample id="1"></sample>
   <sample id="2"></sample>
   .
   .
   .
   .    
</Order>
.
.
.
.
</Orders>

I need to send this xml to the batch component in my flow. My batch should accept each record in the following manner

  Record1:1st Order
  Record2:2nd Order
  Record3:3rd Order
  .
  .
  .
  .

Now please let me know what coding do I need to do in my DataWeave(Transform message) component to acheive this scenario.I tried multiple ways but no result. I am trying this from past few days and got tired. Please help me in this regard!! Please share the sample code.

Output should look like:

1) Order  ---- Object
          sample-----Object
              -sampleValue(i.e; value of 'sample' element)
              -sampleidValue(i.e; value of attribute of 'sample')
          sample-----Object
              -sampleValue(i.e; value of 'sample' element)
              -sampleidValue(i.e; value of attribute of 'sample')
            .
            .
            .
            .
2) Order  ---- Object
          sample-----Object
              -sampleValue(i.e; value of 'sample' element)
              -sampleidValue(i.e; value of attribute of 'sample')
          sample-----Object
              -sampleValue(i.e; value of 'sample' element)
              -sampleidValue(i.e; value of attribute of 'sample')
            .
            .
            .
            .

Inside Batch:

I do have another dataweave component that transforms this collection to csv.

The input of this dataweave component is each object from this collection. The data is extracted from this object and written to the CSV file.

Updated Question:

The issue I am facing is Each Object in the collection is of structure

 Order  ---- Object
          sample1-----Object
              -sampleValue(i.e; value of 'sample' element)
              -sampleidValue(i.e; value of attribute of 'sample')
          sample2-----Object
              -sampleValue(i.e; value of 'sample' element)
              -sampleidValue(i.e; value of attribute of 'sample')
            .
            .
            .
            .

But the output of my CSV should be:

        sample1.sampleValue   sample3.sampleValue   .    .       .       .(this is 'Order1' object)
            sample2.sampleValue   sample6.sampleValue   .    .       .       .(this is 'Order2' object)
.
.
.
.

I am not able to get the output this way in the CSV file. bcz my 'sample' is also a List of Objects. I tried this in both DataMapper and DataWeave also. Please help me out how to extract the values.


回答1:


Hi Please find the below Data Weave

%dw 1.0
%output application/csv separator=" "
---

payload.Orders.*Order map {
SampleValue1:$.sample.@id,
SampleValue2:$.sample.@id,
SampleValue3:$.sample.@id   
}

The below is the X%dw 1.0

%output application/csv separator=" "

payload.Orders.*Order map {
SampleValue1:$.sample.@id,
SampleValue2:$.sample.@id,
SampleValue3:$.sample.@id   
}

The below is the xml that I have used based on the xml that is shared in the question.

<?xml version="1.0"?>
<Orders>
<Order>
  <sample id="1">test</sample>
</Order>
<Order>
  <sample id="1">test</sample>
</Order>
<Order>
   <sample id="1">test</sample>
</Order>
</Orders>

Hope this helps!



来源:https://stackoverflow.com/questions/32589875/xml-to-list-conversion-in-mule-esb-using-data-weave-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!