Marshaller and Unmarshaller (JAXB - Java Architechture for XML Binding)

 Marshaller allows us to write an object to a xml file. 

https://www.youtube.com/watch?v=ZtQ5-c4nAKY

package main;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Employee {

private String name;
private int number;
private int age;
private String mail;
private String city;

@XmlElement
public void setName(String name) {
this.name = name;
}

@XmlElement
public void setNumber(int number) {
this.number = number;
}

@XmlElement
public void setAge(int age) {
this.age = age;
}

@XmlElement
public void setMail(String mail) {
this.mail = mail;
}

@XmlElement
public void setCity(String city) {
this.city = city;
}

public String getName() {
return name;
}

public int getNumber() {
return number;
}

public int getAge() {
return age;
}

public String getMail() {
return mail;
}

public String getCity() {
return city;
}
}
package main;

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

public class JaxbWriter {
public static void main(String[] args) {
Employee employee = new Employee();
employee.setAge(29);
employee.setName("Parvin");
employee.setCity("Baku");
employee.setMail("etibarliparvin@gmail.com");
employee.setNumber(8204296);

File file = new File("C:\\Users\\LENOVO\\Desktop\\test1.xml");
try {
JAXBContext context = JAXBContext.newInstance(Employee.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(employee, file);
} catch (Exception e) {
e.printStackTrace();
}
}
}
This program creates xml file in the shown directory.
<?xml version="1.0" encoding="UTF-8" standalone="true"?>

-<employee>

<age>29</age>

<city>Baku</city>

<mail>etibarliparvin@gmail.com</mail>

<name>Parvin</name>

<number>8204296</number>

</employee>


Unmarshaller reads from xml file and transfers elements to a java object.
package main;

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

public class JaxbReader {
public static void main(String[] args) {
Employee employee = null;
File file = new File("C:\\Users\\LENOVO\\Desktop\\test1.xml");
try {
JAXBContext context = JAXBContext.newInstance(Employee.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
employee = (Employee) unmarshaller.unmarshal(file);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(employee.getName());
}
}
Another example:
package externals;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
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 java.io.File;

@XmlRootElement(name = "DbConfig")
@XmlAccessorType(XmlAccessType.FIELD)
public class DbConfig {

@XmlElement(name = "url")
private String url;
@XmlElement(name = "username")
private String username;
@XmlElement(name = "password")
private String password;

public String getUrl() {
return url;
}

public DbConfig setUrl(String url) {
this.url = url;
return this;
}

public String getUsername() {
return username;
}

public DbConfig setUsername(String username) {
this.username = username;
return this;
}

public String getPassword() {
return password;
}

public DbConfig setPassword(String password) {
this.password = password;
return this;
}

}
package externals;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;

public class MyConnection2 {

public Connection connect() throws Exception {
try {
JAXBContext context = JAXBContext.newInstance(DbConfig.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
DbConfig dbConfig = (DbConfig) unmarshaller.unmarshal(new File("C:\\Users\\LENOVO\\Desktop\\dbconfig.xml"));
return DriverManager.getConnection(dbConfig.getUrl(), dbConfig.getUsername(), dbConfig.getPassword());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}


Комментарии

Популярные сообщения из этого блога

Lesson1: JDK, JVM, JRE

SE_21_Lesson_11: Inheritance, Polymorphism

SE_21_Lesson_9: Initialization Blocks, Wrapper types, String class