XML Parsing using JAXB

Jaxb can be used for reading xml file and storing it as a java object. It uses the binding classes to bind a schema definition file with the java class objects. Generating the binding classes can be done by installing JAXB and then running the command xjc.bat for windows or xjc.sh for linux. The sample program below demonstrates reading a xml file

The command to generate binding classes using xjc would be –

<JAXB_INSTALL_LOCATION>/bin/xjc.bat <Schema file>

Sample.xml

<?xml version="1.0" encoding="UTF-8"?>
<JavaCollectionUtils>
	<Type name="List" >
		<impl name="arraylist"/>
		<impl name="linkedlist"/>
	</Type>
	<Type name="map">
		<impl name="HashMap"/>
	</Type>
	<Type name="Table">
		<impl name="HashTable"/>
	</Type>
</JavaCollectionUtils>

Sample.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="JavaCollectionUtils" type="JavaCollectionUtilsType"/>
  <xs:complexType name="implType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:string" name="name" use="optional"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="TypeType">
    <xs:sequence>
      <xs:element type="implType" name="impl" maxOccurs="unbounded" minOccurs="0"/>
    </xs:sequence>
    <xs:attribute type="xs:string" name="name" use="optional"/>
  </xs:complexType>
  <xs:complexType name="JavaCollectionUtilsType">
    <xs:sequence>
      <xs:element type="TypeType" name="Type" maxOccurs="unbounded" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

import java.io.File;
import java.util.List;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import jaxbBind.ImplType;
import jaxbBind.JavaCollectionUtilsType;
import jaxbBind.TypeType;


public class JaxbTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try{
			//jaxbBind is the package that contains all the jaxb bind classes
			JAXBContext context = JAXBContext.newInstance("jaxbBind");
			//Create an unmarshaller instance to convert xml to java object
			Unmarshaller unmarsh = context.createUnmarshaller();
			SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
			//specify the schema definition for parsing
			Schema schema = sf.newSchema(new File("sample.xsd"));
			unmarsh.setSchema(schema);
			//unmarshall the xml file
			JAXBElement<JavaCollectionUtilsType> obj = (JAXBElement<JavaCollectionUtilsType>) unmarsh.unmarshal(new File("sample.xml"));
			JavaCollectionUtilsType collectionUtils = (JavaCollectionUtilsType) obj.getValue();
			//get a list of all tags of type `Type`
			List<TypeType> collectionTypes = collectionUtils.getType();
			for(TypeType collectionType: collectionTypes){
				System.out.println(collectionType.getName());
				//get a list of all tags of type `impl` for a particular Type
				List<ImplType> implTypes = collectionType.getImpl();
				for(ImplType implType : implTypes){
					System.out.println(implType.getName());
				}
			}
		}
		catch(Exception e){
			//The program throws exception if the xml does not confirm to the shema defined
			e.printStackTrace();
		}
		
	}

}

One thought on “XML Parsing using JAXB

Leave a comment