0

I create an XML dump file with JAXB, then i go through several transformations and arrive at a XML file that is in a format i desire. Now i want to convert this XML File that is properly escaped and encoded into a JSON file using JAXB.

I am NOT trying to marshall an object to JSON but the contents of a File.

This marshals my object to json:

    JAXBContext jc = JAXBContext.newInstance(Employee.class);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
    marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(employee, System.out);

How do i do this for a file that was created by JAXB and transformation, it is called employeeFormatC.xml

The reason it needs to be from file and not object is because i use various style xslt to format the original xml output. I don't see a reason to do that for the json, when i can just convert an already generated and formatted xml.

codeCompiler77
  • 508
  • 7
  • 22

1 Answers1

1

JAXB isn't intended to convert XML directly to JSON. You can only use JAXB to marshal and unmarshal from representation (XML/JSON) to Java objects.

If you want to convert from your transformed XML to JSON with JAXB, you should create Java objects that reflect your transformed XML, unmarshal the XML to those objects, and then marshal to JSON.

Take a look at this question for non-JAXB solutions.

Community
  • 1
  • 1
Samuel
  • 16,923
  • 6
  • 62
  • 75
  • Okay, say i create a JSON data dump, i still need to transform it, just like i did with the xml. How would i go about this? I assume i can't use XSLT. Can i unmarshall my finished XML maybe and then marshall it into a JSON? – codeCompiler77 Mar 10 '16 at 00:00
  • "Can i unmarshall my finished XML maybe and then marshall it into a JSON?" Yes, that's what I was recommending. Create Java objects that represent your finished XML, unmarshal your finished XML, then marshal to JSON. – Samuel Mar 10 '16 at 00:02