Webservices4PM 17032021
Webservices4PM 17032021
Webservices4PM 17032021
Webservices 4:30PM
Mr. RAGHU
-------------------------------
FB https://www.facebook.com/groups/thejavatemple
Email: javabyraghu@gmail.com
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
---------------------------------------------------------
S#1 Define one class and apply JAX-B Annotations
then class is called as JAX-B class.
*) Note: Normal class objects can not be converted into XML Format.
-----Files----------------------------------
1. JAX-B class = class + JAXB-B Annotations
2. JAX-B Operation code (Marshalling/Unmarshalling)
---------------------------------------------
Q) What is the diff b/w int and Integer?
A)
int - primitive type (default value - zero 0 [need memory])
Integer - Wrapper type(default value - null)
(We can even execute opreations/methods over wrapper type)
-------------------------------
m1(Class<T> clz) { }
ob.m1(A.class);
ob.m1(Class.forName("in.nit.model.A"));
------------------------------
interface A{}
class B implements A{}
A oa = getDynamic();
sysout(oa.getClass().getName());
=============Full code===================
1. Maven Application
pom.xml
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
</dependencies>
2. JAXB Class
package in.nareshit.model;
//ctrl+shift+O
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Student {
public Student() {
super();
}
public Integer getStdId() {
return stdId;
}
public void setStdId(Integer stdId) {
this.stdId = stdId;
}
public String getStdName() {
return stdName;
}
public void setStdName(String stdName) {
this.stdName = stdName;
}
public Double getStdFee() {
return stdFee;
}
public void setStdFee(Double stdFee) {
this.stdFee = stdFee;
}
@Override
public String toString() {
return "Student [stdId=" + stdId + ", stdName=" + stdName + ", stdFee="
+ stdFee + "]";
}
}
3. Test class
package in.nareshit.test;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import in.nareshit.model.Student;
//create unmarshaller
Unmarshaller um = context.createUnmarshaller();
//call unmarshal
Student std = (Student)um.unmarshal(new
File("F:/myxmls/data.xml"));
//print data
System.out.println(std);
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("DONE");
} catch (Exception e) {
e.printStackTrace();
}
}
}
------------------------------------------------------