Thi*_*ilo 9 java xml jaxb eclipselink moxy
我正在使用 EclipseLink的JAXB实现中的一些非标准扩展,为了实现该实现,我必须使用jaxb.properties来配置它.效果很好.
但是,由于构建错误,属性文件未包含在正确的位置,导致使用默认的JAXB,没有任何错误只是继续解析XML文件,忽略非标准扩展,留下我一个非工作豆.
为了使这更加健壮,我想摆脱属性文件并在代码中指定上下文配置.我已经有了EclipseLink的编译时依赖性,因为它们的注释,我不需要在部署时配置这部分(实际上,看到可能出错的地方,我不希望它可配置).
bdo*_*han 13
您可以执行以下操作来获取JAXBContext
没有jaxb.properties
文件的EclipseLink JAXB(MOXy):
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);
}
}
Run Code Online (Sandbox Code Playgroud)