Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to deprecated XmlJsonDataFormat in Apache Camel

In Apache Camel, I was trying to use XmlJsonDataFormat to do a quick conversion from XML to JSON. However the XMLJSON (http://camel.apache.org/xmljson.html) has deprecated so I am not sure what is the best way to do that conversion aside from creating a processor.

Does anyone know of an alternative to XmlJsonDataFormat?

like image 371
namp10010 Avatar asked Feb 28 '18 03:02

namp10010


1 Answers

After some tries, I have managed to get what I need by combining Jaxb and Jackson for unmarshal XML to POJO and then POJO to JSON. As noMad pointed out, I could have tried Dozer (http://camel.apache.org/dozer.html) as well but haven't got time to do so.

    JacksonDataFormat jacksonDataFormat = new JacksonDataFormat();
    jacksonDataFormat.setPrettyPrint(true);
    jacksonDataFormat.enableFeature(SerializationFeature.WRAP_ROOT_VALUE);

    from("file:src/main/resources/xml/in?noop=true").routeId("lixi-to-json-route").
        unmarshal(new JaxbDataFormat(JAXBContext.newInstance(ApplicationBatch.class))).
        marshal(jacksonDataFormat).
        to("file:src/main/resources/xml/out?fileName=${file:onlyname.noext}.json");

Maven dependencies

    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jaxb</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jackson</artifactId>
    </dependency>
like image 171
namp10010 Avatar answered Nov 01 '22 02:11

namp10010