0% found this document useful (0 votes)
20 views4 pages

Webservices4PM 18032021

Uploaded by

Suresh Kamble
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

Webservices4PM 18032021

Uploaded by

Suresh Kamble
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Date : 18/03/2021

Webservices 4:30PM
Mr. RAGHU
-------------------------------------
JAX-B API

@XmlRootElement : Must be applied over class name.

className -- Root Tag


variables -- Child Tags

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

a) @XmlAttribute : By default every variable behaves like tag/element


To convert one variable as attribute use this

--sample code---
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {

@XmlAttribute
private int empId;
private String empName;
private double empSal;
...//set,get..toString..
}
--sample output--
<employee empId="10">
<empName>SAMPLE</empName>
<empSal>200.0</empSal>
</employee>
--------------

*) Note:
Only @XmlRootElement is active by default. Other Annotation are
in disabled Mode. To activate other annotations wrote one
variable(instance variable-feilds) use
@XmlAccessorType(XmlAccessType.FIELD)

*) @XmlTransient : To avoid any variable using in Object--XML Conversion


apply this annotation.

---sample code---
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {

@XmlAttribute
private int empId;
private String empName;
private double empSal;
@XmlTransient
private String empPwd;
//set,get...toString..
}
--sample xml output--
<employee empId="10">
<empName>SAMPLE</empName>
<empSal>200.0</empSal>
</employee>

int id=0; //also a value , that needs memory to stroe


Integer id=null //null is not a value, no memory required.

*) If a child element has no data/no value(ie null)


then such tags are not displayed in XML file.

*) @XmlElement: It is used to provide alias names for variables.

--sample code--
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {

@XmlAttribute(name = "id")
private Integer empId;
@XmlElement(name = "employee-name")
private String empName;
@XmlElement(name="employee-salary")
private Double empSal;
@XmlTransient
private String empPwd;
//set,get..toStrig..
}
Employee created but no data set (id-null,name-null,sal-null)

--sample xml output--


<employee/>
--------------------

@XmlElement(name = "___", required = true) :


This required says element value can not be set to null or empty
we must provide some value.
------------------------------------------------

=========Example==============
1. Model
package in.nit.model;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {

@XmlAttribute(name = "id")
private Integer empId;
@XmlElement(name = "employee-name", required = true)
private String empName;
@XmlElement(name="employee-salary")
private Double empSal;
@XmlTransient
private String empPwd;

public Integer getEmpId() {


return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Double getEmpSal() {
return empSal;
}
public void setEmpSal(Double empSal) {
this.empSal = empSal;
}
public String getEmpPwd() {
return empPwd;
}
public void setEmpPwd(String empPwd) {
this.empPwd = empPwd;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", empName=" + empName + ",
empSal=" + empSal + ", empPwd=" + empPwd + "]";
}

2. Test class
package in.nit.test;

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import in.nit.model.Employee;

public class Test {

public static void main(String[] args) {


try {
Employee emp = new Employee();
emp.setEmpId(10);
emp.setEmpName("SAMPLE");
emp.setEmpSal(200.0);
emp.setEmpPwd("ABCD");

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


Marshaller m = context.createMarshaller();
m.marshal(emp, new File("E:/logs/emps.xml"));
System.out.println("DONE");
} catch (Exception e) {
e.printStackTrace();
}
}
}
---------------------------------------------------

You might also like