Reputation: 661
I am currently trying to use EclipseLink Moxy as my JAXB implementation. I am trying this, because the default implementation included in the JDK seems to have a hard coded indentation level of eight with UTF-8 encoding.
My problem is that it seems that I have to put a jaxb.properties file into every package that contains a JAXB POJO. My JAXB POJOs are generated by xjc, specifically by 'jaxb2-maven-plugin'. The POJOs are generated into numerous packages. Is it somehow possible to set the used implementation without creating redundant jaxb.properties files in these packages?
Upvotes: 2
Views: 4676
Reputation: 148977
I am trying this, because the default implementation included in the JDK seems to have a hard coded indentation level of eight with UTF-8 encoding.
The JAXB reference implementation does have an extension property that allows you to control indenting:
As far as jaxb.properties
, when dealing with multiple packages with a single JAXBContext
only one of the packages needs to include the jaxb.properties
file.
There are a few different things that can be done to make your use of MOXy easier:
MOXy offers a script that wraps XJC that will add the jaxb.properties
file in the appropriate spot.
<ECLIPSELINK_HOME>/bind/jaxb-compiler.sh
You could also leverage the META-INF/services
mechanism to specify MOXy as the default JAXB provider:
javax.xml.bind.JAXBContext
in the directory META-INF/services
javax.xml.bind.JAXBContext
file must be org.eclipse.persistence.jaxb.JAXBContextFactory
import java.io.File;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContextFactory.createContext("com.example.pkg1:org.example.pkg2", null, null);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("input.xml");
Object object = unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(object, System.out);
}
}
Upvotes: 4