1

I want to convert multiple xml object to csv object in apache camel. But I am just able to convert single object. With reference to below and attached code, I have tried some method sI have xml file like this

<company>
<employee>
    <empId>1</empId>
    <empName>peter</empName>
    <sal>56000</sal>
</employee>
<employee>
    <empId>2</empId>
    <empName>mary</empName>
    <sal>96000</sal>
</employee>
<employee>
    <empId>3</empId>
    <empName>alex</empName>
    <sal>96000</sal>
</employee>

and want to convert it to csv. But I only manage to convert one data But i have no clue how to change the whole set

@Override
public void configure() throws Exception {

//      Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();         

    JaxbDataFormat xmlDataFormat = new JaxbDataFormat();                        //1.  XML Data Format
    JAXBContext con = JAXBContext.newInstance(company.class);
    xmlDataFormat.setContext(con);

DataFormat  csv  = new BindyCsvDataFormat(employee.class);


    from("file:C:/inputFolder/.camel/xml?noop=true")
    .unmarshal(xmlDataFormat)
    .split(body(String.class).tokenize(","))

//Unmarshaled debug part 
    .process(new Processor(){
        public void process(Exchange exchange) throws Exception {

            //print string //

            String myString = exchange.getIn().getBody(String.class);
            System.out.println("unmarshalled output" + myString);
            //print string//

            //print array //
        //  Message msg =exchange.getIn() ;
        //  ArrayList<Employee> list = msg.getBody(ArrayList.class);
                    //list.forEach(emp -> System.out.println(emp));
            //print array //
            ////        }}

        }})
//Unmarshaled debug part 


    .marshal(csv)
    .to("file:C:/outputFolder/csv").

    // Marshalled output 
    process(new Processor(){
                    public void process(Exchange exchange) throws Exception {
                        //print string//
                        String myString = exchange.getIn().getBody(String.class);
                       System.out.println("marshalled output" + myString);
                      //print string//

                        //print array //
                        //Message msg =exchange.getIn() ;
                    //  ArrayList<Employee> list = msg.getBody(ArrayList.class);
                    //          list.forEach(emp -> System.out.println(emp));
                        //print array //
                    }})                  
    ;}


}
R.D
  • 226
  • 1
  • 17
Sam Li
  • 91
  • 1
  • 4
  • It seems I can make use of hashmap , but how can i set the value ? key is empId but how about the value ? – Sam Li Aug 16 '18 at 04:08
  • 1
    Why dont you just use XSLT to convert XML to CSV it will use the raw XML and convert to CSV without you having to do one row at a time. See this https://stackoverflow.com/questions/365312/xml-to-csv-using-xslt – Namphibian Aug 16 '18 at 23:34

1 Answers1

0

You have to check what result you get after the unmarshal with jaxb.

I assume you get only one employee because you treat this result as String and split it by comma ,. If there is, as my assumption is, no comma at all, you get only one part from the splitter: the whole input.

If the result from jaxb is a Java collection of employees you can simply tell Camel

.split(body())

and you get each individual employee from the splitter.

If you want to re-collect them after conversion to write a CSV file with all entries of the XML you must add an aggregation strategy to your splitter, see here for an example. Camel will then re-aggregate the converted elements of the consumed XML file automatically.

Very nice isn't it?

burki
  • 6,741
  • 1
  • 15
  • 31