【问题标题】:Can I replace jaxb.properties with code?我可以用代码替换 jaxb.properties 吗?
【发布时间】:2011-10-21 07:06:27
【问题描述】:

I am using some non-standard extensions 来自 EclipseLink 的 JAXB 实现,为了启用该实现,我必须使用 jaxb.properties 对其进行配置。效果很好。

但是,由于构建错误,属性文件没有包含在正确的位置,导致使用默认的 JAXB,它没有任何错误只是继续解析 XML 文件,忽略非标准扩展名,离开我有一个不工作的豆子。

为了使它更健壮,我想摆脱属性文件并在代码中指定上下文配置。由于它们的注释,我已经对 EclipseLink 有一个编译时依赖项,并且我不需要在部署时可配置这部分(事实上,看看会出现什么问题,我不希望它可配置)。

【问题讨论】:

  • 考虑替代方案:在构建后期测试 jaxb.properties 的存在。

标签: java xml jaxb eclipselink moxy


【解决方案1】:

您可以执行以下操作来获取没有 jaxb.properties 文件的 EclipseLink JAXB (MOXy) JAXBContext

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.eclipse.persistence.jaxb.JAXBContextFactory;

public class Demo {

    public static void main(String[] args) throws Exception {
        //JAXBContext jc = JAXBContext.newInstance(Animals.class);
        JAXBContext jc = JAXBContextFactory.createContext(new Class[] {Animals.class}, null);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum6871469/input.xml");
        Animals animals = (Animals) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(animals, System.out);
    }

}

【讨论】:

  • 如何在上面的代码中设置 MarshallerProperties.JSON_INCLUDE_ROOT 属性?
猜你喜欢
  • 1970-01-01
  • 2020-02-24
  • 2017-07-09
  • 2021-07-07
  • 2017-07-07
  • 2011-07-17
  • 1970-01-01
  • 2014-08-22
  • 2017-12-19
相关资源
最近更新 更多