Transforming Json to xml using camel xml-jason

Solution Unverified - Updated -

Environment

  • Red Hat Fuse
    • 6.3.0

Issue

  • Namespace are getting lost during marshaling from json to xml
  • during this process xmlns namespace from request packet are getting lost .
  • I am doing marshalling using following code
  JAXBContext jc = JAXBContext.newInstance(xxxDocument.class);
                Marshaller marshaller = jc.createMarshaller();
                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                StringWriter writer=new StringWriter();
                marshaller.marshal(xxxDocument,writer);
                String body=writer.toString();

Resolution

  • XML to Jason and Jason to XML transformation can be done using camel
    camel-xmljson component
  • Please find the route below
 // create JSON to XML data format
        XmlJsonDataFormat json2XmlFormat = new XmlJsonDataFormat();
        json2XmlFormat.setEncoding("UTF-8");
        json2XmlFormat.setRootName("rootElementName");

  // Changes JSON data found in folder to XML  
        from("file:src/data?noop=true").unmarshal(json2XmlFormat)
        .log("${body}").to("direct:output");

        from("direct:output")
        .to("file:src/output");

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.