Reputation: 526
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.
Upvotes: 0
Views: 5104
Reputation: 17171
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.
Upvotes: 1