Generate XML from XSD Java JAXB Example

Generate XML XSD Java JAXB Example

In previous tutorial you saw how to generate Java class from XSD file. In this tutorial I will show you complete example of generating classes start from XML –> XSD –> Java Classes –> XML.

Note: Please see this tutorial to generate XSD from XML file and below is XSD which we will use to generate Java Classes and finally XML where data will be populated dynamically.

Tools: You don’t need anything extra except Eclipse latest version and JDK 1.8.

  • XML file:
<?xml version="1.0"?>
<Company>
  <Employee>
      <FirstName></FirstName>
      <LastName></LastName>
      <ContactNo></ContactNo>
      <Email></Email>
      <Address>
           <City></City>
           <State></State>
           <Zip></Zip>
      </Address>
  </Employee>
</Company>
  • Converted XSD File from above XML:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
  <xs:element name="Company" type="CompanyType"/>
  <xs:complexType name="AddressType">
    <xs:sequence>
      <xs:element type="xs:string" name="City"/>
      <xs:element type="xs:string" name="State"/>
      <xs:element type="xs:string" name="Zip"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="EmployeeType">
    <xs:sequence>
      <xs:element type="xs:string" name="FirstName"/>
      <xs:element type="xs:string" name="LastName"/>
      <xs:element type="xs:string" name="ContactNo"/>
      <xs:element type="xs:string" name="Email"/>
      <xs:element type="AddressType" name="Address"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="CompanyType">
    <xs:sequence>
      <xs:element type="EmployeeType" name="Employee"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
  • To understand better I will create maven project (XSDToJava) and below is final structure:

Generate XML XSD Java JAXB Example

  • Classes inside com.javahonk package will be generated by XSD file::

Generate XML XSD Java JAXB Example

  • You could also user below command to generate Java Classes:

xjc -d ../../../src/main/java -p com.javahonk JavaHonk.xsd

  • Generated classes:

Generate XML XSD Java JAXB Example

  • Now we will populate some data to Java Object and finally will generate XML string using JavaToXML.java class:
package com.javahonk.javatoxml;

import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;

import com.javahonk.AddressType;
import com.javahonk.CompanyType;
import com.javahonk.EmployeeType;
import com.javahonk.ObjectFactory;

public class JavaToXML {
	
	private static final ObjectFactory objectFactory = new ObjectFactory();

	public static void main(String[] args) throws JAXBException {
		
		CompanyType companyType = objectFactory.createCompanyType();
		EmployeeType employeeType = objectFactory.createEmployeeType();
		AddressType addressType = objectFactory.createAddressType();
		
		addressType.setCity("NY");
		addressType.setState("ny");
		addressType.setZip("10056");
		
		employeeType.setAddress(addressType);
		employeeType.setContactNo("23654");
		employeeType.setEmail("javahonk@javahonk.com");
		employeeType.setFirstName("Java");
		employeeType.setLastName("Honk");
		
		companyType.setEmployee(employeeType);
		
		JAXBContext jaxbContext = JAXBContext.newInstance("com.javahonk");
		Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
		jaxbMarshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
		
		JAXBElement<CompanyType> jaxbElement = new JAXBElement<CompanyType>(new QName("Company"), CompanyType.class, companyType);
		
		StringWriter sw = new StringWriter();
		
		jaxbMarshaller.marshal(jaxbElement, sw);
		jaxbMarshaller.marshal(jaxbElement, System.out );

	}

}
  • To run right click JavaToXML.java –> Run As –> Java Application you will see below output on console:

Generate XML XSD Java JAXB Example

Download project: XSDToJava

For more information please visit Oracle documentation here

Leave a Reply

Your email address will not be published. Required fields are marked *