javaxmlapache-camelcamel-http

How to merge two xml tags into one xml using apache camel bindy


My problem is I have two different positions in my flat file with "|" seperated string. Position 2 and 3 refer to description. I want to generate xml with both descriptions appended to one description and return one DESC tag with both descriptions.

Input Source:

UN|2.1  |AEROSOLS

Expected Output:

<TEST>
<STATE>UN</STATE>
<DESC>2.1 AEROSOLS</DESC>
</TEST>

OUTPUT I'M GETTING:

    <TEST>
    <STATE>UN</STATE>
    <DESC_ONE>2.1 AEROSOLS</DESC_ONE>
    <DESC_TWO>AEROSOLS<DESC_TWO>
    </TEST>

TEST.java

@Data
public class TEST {


    @DataField(pos = 1, name = "STATE", required = true)
    private String STATE;

    @DataField(pos = 2, name = "DESC",required = true)
    private String DESC_ONE;

    @DataField(pos = 3, name = "DESC",required = true)
    private String DESC_TWO;


}

Converter.java

                            from(SOURCE_INPUT_PATH).
                            log("Received input from file and body is ${body}").
                            unmarshal(bindyBeanConfig.bindyCsvDataFormat3280()).
                            process(new Processor()  {
                                @Override
                                public void process(Exchange exchange) throws Exception {

                                    TEST test = exchange.getIn().getBody(TEST.class);
                                    DESC_ONE descOne = test.getTEST();
                                    DESC_TWO descTwo = descOne.getDESC_ONE();

                                    String description = descOne.getDESC_ONE() + descTwo.getDESC_TWO();

                                    descOne.set_DESCONE(description);

                                    exchange.getIn().setBody(descTwo);

                                }
                            }).

Solution

  • I don't know much about apache-camel , nonetheless you can have another DataField in TEST say DESC , and you can set DESC instead of DESC_ONE in Converter.java , further before calling exchange.getIn().setBody(descTwo); you should set DESC_ONE and DESC_TWO to null.