Sunday, July 01, 2007

xjc.exe ?

Today I was looking some options in jdk/bin and suddenly I came across a file name xjc.exe in JDK6(I guess Mustang is no more official name of JDK6). Quickly I checked the existence of this file in JDK 5.0 and JDK 1.4.2. File was not present. Goggling didn't provided too much of help and I tried with myself :)

I just copied an XSD file from somewhere:

FileName: unknowntest.xsd (some problem with blogger, not able to take xml code :D, so pasting the image)










and passed this XSD file into xjc option:
Usage: xjc [-options ...] ... [-b ] ...

> xjc unknowntest.xsd

parsing a schema...
compiling a schema...
generated\Country.java
generated\ObjectFactory.java

Ah I got 2 Java files yeppp - Country.java and ObjectFactory.java
Look at these 2 files ( I am removing the documentation part of it, to make it small)

package generated;

import java.math.BigDecimal;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"name",
"pop"
})
@XmlRootElement(name = "country")
public class Country {

@XmlElement(required = true)
protected String name;
@XmlElement(required = true)
protected BigDecimal pop;

/**
* Gets the value of the name property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getName() {
return name;
}

/**
* Sets the value of the name property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setName(String value) {
this.name = value;
}

/**
* Gets the value of the pop property.
*
* @return
* possible object is
* {@link BigDecimal }
*
*/
public BigDecimal getPop() {
return pop;
}

/**
* Sets the value of the pop property.
*
* @param value
* allowed object is
* {@link BigDecimal }
*
*/
public void setPop(BigDecimal value) {
this.pop = value;
}

}

and objectFactory don't have too much in it. Just creating a Country class instance. Oh so it is something cool, its making all the getter and setter method for me, what else I need :).

Remember, such type of methods are too much in demand when you are writing any jsp page. So, if you are clear with the data type, just make an xsd file and xjc will convert in into java code.

I still doubt what xjc refers for ... may be xml to java converter :)

7 comments:

ubuntu said...

this has made me laugh,

we needed exactly this for our project, and we did it through XML beans, good u explored, next time i'll use this.

Vaibhav said...

Ha ha ha !

Ya I guess this is one of the most common demands, really surprised that I am not getting any document for it.

ubuntu said...

check out Jikes, it is a java compiler with a difference.
Check my post on it.

Abhishek said...

What do IDEs use for generating the getter and setter methods? XML Beans? and I think it should be pretty basic because this feature has been present in most of the IDEs since quite long. Even my dear Dr.Java :)

Bipin "3~" Upadhyay said...

I guess Mustang is no more official name of JDK6
Is it official?
Why in the hell, BTW?

Vaibhav said...

yes no idea.. but they have remove mutang from official name and declared it to be jdk6 only.

Vaibhav said...

Yes Abhishek, it should be present in jdk older versions also