-1

I have to read a CSV file with 6 different structured formats and convert to XML format. I need a help to read multi-structured CSV file in mule.

Sample input:

01,12345,Cap,01-02-2017
02,12345,subject1, subject2,subject3,subject4,subject5
03,12345,65432,45,ABS
04,12345,ABC,DEF,,
05,12345,5325,ABC,
06,12345,87.9,ASDF,LKJ
06,12345,99,ABC,WERT

Expected Output:

<Root>
  <Sample>
    <Number>12345</Number>
    <B>Cap</B>
    <C>01-02-2017</C>
  </Sample>
  <Example>
    <Sub>
      <Number>12345</Number>
      <S1>subject1</S1>
      <S2>subject2</S2>
      <S3>subject3</S3>
      <S4>subject4</S4>
      <S5>subject5</S5>
    </Sub>
    <Sub1>
      <Number>12345</Number>
      <A1>65432</A1>
      <A2>45</A2>
      <A3>ABS</A3>
   </Sub1>
   <Sub2>
     <Number>12345</Number>
     <B1>ABC</B1>
     <B2>DEF</B2>
     <B3/>
   </Sub2>
   <Sub3>
     <Number>12345</Number>
     <C1>5325</C1>
     <C2>ABC</C2>
   </Sub3>
   <Sub4>
    <Sub_rec>
      <Number>12345</Number>
      <D1>87.9</D1>
      <D2>ASDF</D2>
      <D3>LKJ</D3>
    </Sub_rec>
    <Sub_rec>
      <Number>12345</Number>
      <D1>99</D1>
      <D2>ABC</D2>
      <D3>WERT</D3>
    </Sub_rec>
  </Sub4>
 </Example>
</Root>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
SreenivasB
  • 11
  • 1
  • 3
    It's much better to include all necessary information in the answer itself. It will also allow better formatting. See [How to ask](http://stackoverflow.com/help/how-to-ask). – yeputons Feb 03 '17 at 10:42
  • 1
    Possible duplicate of [Can ysomeone give example how to convert csv to xml in mule?](http://stackoverflow.com/questions/24604599/can-ysomeone-give-example-how-to-convert-csv-to-xml-in-mule) – thodic Feb 03 '17 at 12:16
  • Can you differentiate the 6 different input structures in a new line or so?? It is hard to read the delimitation between them. – Senthil c Feb 03 '17 at 12:41

1 Answers1

0

Assuming data will not change, dataweave transform is

%dw 1.0
%output application/xml
---
root: {
    sample: {
        Number: payload[0][0]
    },
    Example: {
        sub : {
            Number: payload[1][0]
        },
        sub1 : {
            Number: payload[2][0]
        },
        sub2 : {
            Number: payload[3][0]
        },
        sub3 : {
            Number: payload[4][0]
        },
        sub4 : {
            Sub_rec: {
                Number: payload[5][0]
            },
            Sub_rec: {
                Number: payload[6][0]
            }
        }
    }
}

Other values can be filled using S1: payload[1][3] and so on.

Second way is using map, though it also assumes data is static, bit more flexble

root: {
    sample: {
        Number: payload[0][0]
    },
    Example: {
        (payload[1..-2] map ((row, indexOfRow) -> {
            Sub : {
                Number: row[0]
            } when indexOfRow == 0
            otherwise {
                Number: row[0]
            } when indexOfRow == 1
            otherwise {
                Number: row[0]
            } when indexOfRow == 2
            otherwise {
                Number: row[0]
            } when indexOfRow == 3
                otherwise {
                    (payload[-2..-1] map {
                        sub_rec: {
                            Number: $[2]
                        }
                })
            }
        }))
    }
}
Abhay
  • 314
  • 1
  • 2
  • 11