8

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?

kudlatiger
  • 3,028
  • 8
  • 48
  • 98
namp10010
  • 123
  • 1
  • 10
  • You can take a look at Dozer. It should be able to convert between XML and JSON. – Erik Karlstrand Feb 28 '18 at 08:28
  • I just saw your comment after investigating the combination of jaxb and jackson. I managed to get it working and will keep Dozer in mind as it looks like a quite convenient mapper/converter. – namp10010 Feb 28 '18 at 22:51

1 Answers1

2

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>
namp10010
  • 123
  • 1
  • 10
  • This won't compile for me. Do you still have the project? It says there are no methods on JacksonDataFormat class – OldProgrammer May 31 '23 at 20:55
  • @OldProgrammer, sorry it's been a while so I don't have the source code. However, did you include all the required dependencies? The error may mean that it could not find the dependency. – namp10010 Jun 08 '23 at 03:11