JAXB tutorial part 2: generating classes from XML schema

How to generate JAXB classes from XML Schema (XSD) in Eclipse.

From previous screencast I have two files: sitemap.xml, which I want to read and sitemap.xsd, which describes format of sitemap.xml

First I will create a new JAXB project.

You must have Eclipse for Java EE developers, because it contains plugin Web Tools Platform (WTP), that has support for JAXB. If you don't have Eclipse for Java EE developers, you can download it separatelly or install plugin WTP to your Eclipse.

Be sure to have associated a JDK with your project, not simple JRE, because JAXB uses for class generation an application xjc.exe, which is only in JDK, so without associated JDK it won't work.

In Java EE perspective notice this "JAXB Context".

Now right click your project, choose other and select "JAXB Classes from Schema". Select sitemap.xsd and specify package where will be generated classes. This will generate several classes such as Urlset and TUrl. If you go to sitemap.xsd, you can notice that these classes are also defined here. In classes notice that class Urlset is annotated with annotation @XmlRootElement and class TUrl is annotated with annotation @XmlType.

comments powered by Disqus


Free online Java web application examples

Want to develop whole web applications which use database, security and more? See my github account, where are lot's of example projects: