【发布时间】:2016-03-09 23:34:18
【问题描述】:
我用 JAXB 创建了一个 XML 转储文件,然后经过多次转换,得到一个我想要的格式的 XML 文件。现在我想使用 JAXB 将这个经过正确转义和编码的 XML 文件转换为 JSON 文件。
我不是试图将对象编组为 JSON,而是将文件的内容编组。
这会将我的对象编组为 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);
我如何为由 JAXB 和转换创建的文件执行此操作,它称为 employeeFormatC.xml
它需要来自文件而不是对象的原因是因为我使用各种样式 xslt 来格式化原始 xml 输出。当我可以转换一个已经生成并格式化的 xml 时,我看不到为 json 这样做的理由。
【问题讨论】: