I am using some non-standard extensions from EclipseLink's implementation of JAXB, and to enable that implementation, I have to configure it using jaxb.properties. Works well.
However开发者_Go百科, due to a build error, the properties file was not included in the proper place, resulting in the default JAXB being used, which without any errors just continued to parse the XML file, ignoring the non-standard extension, leaving me with a non-working bean.
To make this more robust, I'd like to get rid off the properties file and specify the context configuration in code. I already have a compile-time dependency on EclipseLink because of their annotations, and I do not need this part configurable at deploy time (in fact, seeing what can go wrong, I do not want it configurable).
You could do the following to get an EclipseLink JAXB (MOXy) JAXBContext
without a jaxb.properties
file:
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);
}
}
精彩评论