Convert XML to Java Classes

To convert a given XML file to Java Classes we need two tools:

  1. xsd-gen
  2. xjc

xsd-gen

xsd-gen is a tool which comes as part of “The Joy of Unix in Windows Tool Bundle”:

Install WizTools and add the location of bin folder to the path C:\Program Files (x86)\WizTools_org\Cli_Tools_Bundle\bin

xjc

xjc comes along with jdk and you may find it in jdk bin path. You have to add jdk bin to the path.

Once above tools are set. We can open cmd [command prompt] and enter following command.

xsd-gen xmlFileName.xml > xsdFileName.xsd

 

This will generate an xsd file which will be used by xjc to convert it to classes.

Tree.xml
Tree.xml
Tree.xsd
Tree.xsd

Once xsd is generated we can use xjc to generate java classes.

xjc -p com.package.name xsdFileName.xsd

 

This will create folder structure as per the package specified and necessary java classes inside the folder.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s