Webservices4PM 17032021

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

Date : 17/03/2021

Webservices 4:30PM
Mr. RAGHU
-------------------------------
FB https://www.facebook.com/groups/thejavatemple
Email: javabyraghu@gmail.com

JAX-B API - Annotations and Code

Marshalling : Converting Java Object to XML Format.


Unmarshalling : Converting XML to Java Object Format.

JAX-B API Dependencies (JARS)


[ jaxb-api , jaxb-impl, jaxb-core ]

<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.

S#2 Define JAXBContext object using JAX-B class.

*) Every Context is bounded to one class.


This context supports JAX-B Operations.

S#3 using Context object call method

createMarshaller() --> Marshaller(I)

createUnmarshaller() --> Unmarshaller(I)

S#4 Then call method

For Objext --> XML File (use Marshaller)


marshal(obj,file)

For XML File --> Object (use Unmarshaller)


unmarshal(file):obj

-----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 {

private Integer stdId;


private String stdName;
private Double stdFee;

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;

public class Test {

public static void main(String[] args) {


try {
// create context object for Student
JAXBContext context = JAXBContext.newInstance(Student.class);

//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();
}
}

public static void main1(String[] args) {


try {
Student sob = new Student();
sob.setStdId(100);
sob.setStdName("SAMPLE");
sob.setStdFee(1200.0);

// create context object for Student


JAXBContext context = JAXBContext.newInstance(Student.class);

// create Marshaller object


Marshaller m = context.createMarshaller();
System.out.println(m.getClass().getName());

//call marshal method


m.marshal(sob, new File("F:/myxmls/data.xml"));

System.out.println("DONE");
} catch (Exception e) {
e.printStackTrace();
}
}
}
------------------------------------------------------

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy