java/j2ee tips and blueprints
java/j2ee tips and blueprints
It is a very sad thing that nowadays
there is so little useless information.
Oscar Wilde
JAXB quickstart via maven2
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
<dependencies> <!-- ....--> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.1</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.1.3</version> </dependency> </dependencies>
2. Adding jaxb2-mojo-plugin
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<packageName>com.example.myschema</packageName> <!-- The name of your generated source package -->
<outputDirectory>${basedir}/src/main/generated-source</outputDirectory>
</configuration>
</plugin>
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 jxb2: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<PurchaseOrderType> element = factory.createPurchaseOrder(type);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(element, System.out);
}
}
and output is
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <purchaseOrder> <comment>comment</comment> <items> <item partNum="test"> <productName>productname</productName> <quantity>2</quantity> <comment>part comment</comment> </item> </items> </purchaseOrder>
Additional references:
jaxb2-maven-plugin/xjc-mojo
maven jaxb plugins
sun official jaxb reference












Thanks, this really helped!