Friday, February 28, 2014

REST API Knowledge JAXB, JAX RS, etc.

What JAXB Does?

1. Generating Java Code from XML Schema

  Step 1: Create XSD File

<?xml version="1.0"?>
<xs:schema version="1.0"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified"
           targetNamespace="http://a.bc/xsd/TestScheme"
           xmlns="http://a.bc/xsd/TestScheme">
    <xs:element name="govind" type="Student"/>

    <xs:complexType name="Student">
        <xs:sequence>
            <xs:element name="sname" type="xs:string"/>
            <xs:element name="lname" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

Step 2. Run below command

[your java home dir]\bin\xjc.exe c:\test_schema.xsd

Step3 : Check out the generated output
C:\Program Files (x86)\IBM\SDP\jdk\bin>xjc.exe c:\test_schema.xsd
parsing a schema...
compiling a schema...
bc\a\xsd\testscheme\ObjectFactory.java
bc\a\xsd\testscheme\PersonInfoType.java
bc\a\xsd\testscheme\package-info.java

ObjectFactory.java
@XmlRegistry
public class ObjectFactory {

    private final static QName _Govind_QNAME = new QName("http://a.bc/xsd/TestScheme", "govind");

     public ObjectFactory() {  }
    public Student createStudent() {
        return new Student()}

    @XmlElementDecl(namespace = "http://a.bc/xsd/TestScheme", name = "govind")
    public JAXBElement<Student> createGovind(Student value) {
        return new JAXBElement<Student>(_Govind_QNAME, Student.class, null, value);
    }}

package-info.java
@javax.xml.bind.annotation.XmlSchema(namespace = "http://a.bc/xsd/TestScheme", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package bc.a.xsd.testscheme;

Student.java
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Student", namespace = "http://a.bc/xsd/TestScheme", propOrder = {
    "sname",
    "lname"
})
public class Student {
    @XmlElement(required = true)
    protected String sname;
    @XmlElement(required = true)
    protected String lname;

    public String getSname() {
        return sname;
    }    public void setSname(String value) {
        this.sname = value;
    }

    public String getLname() {
        return lname;
    }
    public void setLname(String value) {
        this.lname = value;
    }

Points:

JAXB Limitation.
I worked on JAXB, as per my opinion its a nice way of dealing with data between XML and Java objects. The Positive sides are its proven and better in performance and control over the data during runtime. With a good usage of built tools or scripts it will takes away lot of coding efforts.
I found the configuration part is not a straight away task, and spent hours in getting the development environment setup.
However I dropped this solution due to a silly limitation I faced. My XML Schema Definition ( XSD ) has a attribute/element with name "value" and that I have to use XSD as it is. This very little constraint forced the my binding step XJC failed with a Error "Property 'Value' already used."
This is due to the JAXB implementation, the binding process tries to create Java objects out of XSD by adding few attributes to each class and one of them being a value attribute. When it processed my XSD it complained that there is already a property with that name.
 
http://theopentutorials.com/examples/java/jaxb/generate-java-class-from-xml-schema-in-eclipse-ide/

1. JAXB (xjc utility) is installed together with JDK6 by default.
2. Using Eclipse IDE:
  1. Download http://java.net/downloads/jaxb-workshop/IDE%20plugins/org.jvnet.jaxbw.zip
  2. Extract the zip file .
  3. Place the org.jvnet.jaxbw.eclipse_1.0.0 folder into .eclipse\plugins folder
  4. Restart the eclipse.
  5. Right click on XSD file and you can find contect menu. JAXB 2.0 -> Run XJC .
  1. copy the xsd into a new/existing project.
  2. Make sure you have JAXB required JARs in you classpath. You can download one here.
  3. Right click on the XSD file -> Generate -> JAXB classes. 

 


2.  Java object to / from XML file

Example link

Eclipse Article

Eclipse

No comments:

Post a Comment