JAXB tutorial part 1: XML Binding explained

XML Binding basics explained: JAXB, XML, XSD, marshalling and unmarshalling. Introduction to sitemap.xml file.

Often you want to work with XML files in Java. You can do this using DOM, SAX or XML binding. Now I will show you how to use XML binding.

In this technology there's explored one notion - like objects are instances of classes, XML files are instances of XSD files (also known as XML Schema files). If you have a XSD file, you can automatically generate classes. These classes will be annotated with annotations from JAXB library, which is part of JRE. Next you can marshall your objects to xml files or unmarshall data from XML to objects. This is called XML binding.

Btw. if you have a XML file you want to work with and don't have a XSD file, don't despair. There are ways to almost automatically construct XSD file from your XML file (for example Rinzo plugin for Eclipse). Also you can work with JAXB the other way around - you can construct classes with annotations and next you can generate XSD from these classes.

Now I'd like to show you how to parse one XML file, that has a well-known format: sitemap file. In this file there's tag <urlset>, where are URLs of current website (in tag <url>). Each URL has tag <loc>, which is a hyperlink.

I would like to get all URLs from this sitemap. First I will save this sitemap. Next we need a XSD file. Google "sitemap xsd" and save it. If your browser saved it as sitemap.xsd.xml, delete "dot xml".

In the next screencast I will show you how to work with JAXB in Eclipse.