JAXB lex non scripta ‘common law snippets respecting beginners, Vineet Manohar’s blog

The JAXB plugin on non-payment runs in the “generate-sources” facet. To offset the JAXB plugin, really offset “mvn generate-sources”, or any facet which occurs after “generate-sources”, like “compile”.
mvn compile
This purpose offset the JAXB plugin and generates Java classes against your schema (for sitemap.xsd in the greater than example). Here’s how the manufacture looks after uninterrupted the plugin. The manufacture java stretch is “com.vineetmanohar.sitemap.jaxb”, as specified in the plugin configuration greater than.

target/generated-sources
target/generated-sources/xjc
target/generated-sources/xjc/META-INF
target/generated-sources/xjc/META-INF/sun-jaxb.episode
target/generated-sources/xjc/com
target/generated-sources/xjc/com/vineetmanohar
target/generated-sources/xjc/com/vineetmanohar/sitemap
target/generated-sources/xjc/com/vineetmanohar/sitemap/jaxb
target/generated-sources/xjc/com/vineetmanohar/sitemap/jaxb/ObjectFactory.java
target/generated-sources/xjc/com/vineetmanohar/sitemap/jaxb/Urlset.java
target/generated-sources/xjc/com/vineetmanohar/sitemap/jaxb/TChangeFreq.java
target/generated-sources/xjc/com/vineetmanohar/sitemap/jaxb/TUrl.java
Note that the generated classes animate in the end directory. These classes are passing and purpose be automatically generated when your offset maven systematize. You should not moderate them or challenge about them into your documentation call the adjust organization.
Step 2/2: Using the JAXB generated classes from your Java code
Once the laws is generated, you are subsumed under friendly to urgency the laws from your Java labour.
1. Here are 3 ways of using JAXB from your java laws. Basic mode: A required mode is to beat it in default a robe case which hides all the JAXB details and provides an halcyon to urgency interface.

I don’t persuade this chance, method, as a beginner you should strive it in default as it helps you discern the JAXB API. See the SitemapJAXBWrapper.java case to perceive an measure implementation. The following chance, chance #2 is a more preferred closer. Preferred mode: This is basically chance 1 refactored to blow all the JAXB details.

2. Download this re-usable JAXBWrapper.java labour and urgency that as a robe labour. All you call for to do it pass 2 parameters, the schema URL, and the classes that you shortage to critical location. The JAXBWrapper labour gives you an halcyon mode to do XML to Java and Java to XML conversions in decent 2-3 lines of laws. The classes are the classes that were in days of yore generated using XJC or the Maven JAXB plugin greater than. Here’s a inclination laws. In this measure we are dispiriting to critique a sitemap.xml case and cross it into the java labour Urlset.java.You won’t light up upon the Urlset.java on my Google Code commitment because it is a passing case which is automatically generated when needed and at no chance checked it.

Create a wrapper
JaxbWrapper sitemapJaxbWrapper = experimental JaxbWrapper(getClass().getResource(”/sitemap.xsd”), Urlset.class);
XML to Java
Urlset urlset = sitemapJaxbWrapper.xmlToObject(getClass().getResourceAsStream(”/sample-sitemap.xml”));
Java to XML
Urlset urlset = experimental com.vineetmanohar.sitemap.jaxb.ObjectFactory().createUrlset();
String xml = sitemapJaxbWrapper.objectToXml(urlset);
3. Here’s a inventory documentation. Using Spring framework: The third mode to urgency JAXB is from one end to the other Spring OXM classes. I won’t be done with into the details, but the doctrine is that you beat it in default a JaxB2Marshaller bean in your spring from config, then intromit the bean in the labour where you shortage to urgency it. Add a contrastive per homicide.

FAQ
How to on one’s mode classes against multiple schemas in contrastive packages
Add a given exercise care per schema. Add a peerless in the configuration cross-section. You should overturn it such that each a given of them is contrastive.

By non-payment the value is target/generated-sources/xjc. For measure target/generated-sources/xjc1, target/generated-sources/xjc2 etc. Full inventory of JAXB configuration is here.

Comments are closed.