JAXB quickstart via maven2
Tuesday, January 22, 2008 4:27Posted in category HowTo & Tutorial
Image via Wikipedia
JAXB is a java framework to XML-Java transformations.
JAXB is 2.1 has a very simple API like castor and jibx And annotation driven transformers. Although those configurations can be written manually JAXB has a code generator to generate java files from XSD files. Here is sample JAXB tutorial using maven2:
1. extending project dependency
<!-- ....--> javax.xml.bind jaxb-api 2.1 com.sun.xml.bind jaxb-impl 2.1.3
2. Adding jaxb2-mojo-plugin
org.codehaus.mojo
jaxb2-maven-plugin
xjc
com.example.myschema <!-- The name of your generated source package -->
${basedir}/src/main/generated-source
ref:http://mojo.codehaus.org/jaxb2-maven-plugin/xjc-mojo.html
3. adding your XSD files under /src/main/xsd
sample file: model1.xsd
4. generate your java file from XSD
mvn jaxb2:xjc
sample java code:
purchaseordertype.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "PurchaseOrderType", propOrder = {
"shipTo",
"billTo",
"comment",
"items"
})
public class PurchaseOrderType {
@XmlElement(required = true)
protected USAddress shipTo;
@XmlElement(required = true)
protected USAddress billTo;
protected String comment;
@XmlElement(required = true)
protected Items items;
@XmlAttribute
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"productName",
"quantity",
"usPrice",
"comment",
"shipDate"
})
public static class Item {
@XmlElement(required = true)
protected String productName;
protected int quantity;
@XmlElement(name = "USPrice", required = true)
protected BigDecimal usPrice;
protected String comment;
protected XMLGregorianCalendar shipDate;
@XmlAttribute(required = true)
protected String partNum;
5. And test your code
public class JAXBTest extends TestCase {
public void testMarshall() throws JAXBException {
JAXBContext jc = JAXBContext.newInstance("com.example.myschema");
ObjectFactory factory = new ObjectFactory();
PurchaseOrderType type = factory.createPurchaseOrderType();
type.setComment("comment");
Items items = factory.createItems();
Items.Item o = factory.createItemsItem();
o.setPartNum("test");
o.setComment("part comment");
o.setProductName("productname");
o.setQuantity(2);
items.getItem().add(o);
type.setItems(items);
JAXBElement element = factory.createPurchaseOrder(type);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(element, System.out);
}
}
and output is
comment productname 2 part comment
Additional references:
jaxb2-maven-plugin/xjc-mojo
maven jaxb plugins
sun official jaxb reference
Related posts:
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=9e3e92c9-5772-4782-8846-17a903b62247)














DaShaun says:
May 2nd, 2008 at 3:14 pm
Thanks, this really helped!
markatharvest says:
December 23rd, 2008 at 6:59 am
Thank you, was very confused with various plugins available, till the time I found your blog.
thanks again for sharing
admin says:
December 23rd, 2008 at 9:34 am
enjoy it Mark
gungus says:
June 2nd, 2009 at 1:29 pm
Hi, nicely presented.
Could you please correct the “mvn jxb2:xjc” to “mvn jaxb2:xjc”.
Thanks.
//GG
altuure says:
June 9th, 2009 at 9:03 am
thanks gungus, I have updated the post