An introduction to JAXB
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.jaxb;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAttribute;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import javax.xml.bind.annotation.XmlTransient;
|
||||
import javax.xml.bind.annotation.XmlType;
|
||||
|
||||
@XmlRootElement(name = "book")
|
||||
@XmlType(propOrder = { "id", "name", "date" })
|
||||
public class Book {
|
||||
private Long id;
|
||||
private String name;
|
||||
private String author;
|
||||
private Date date;
|
||||
|
||||
@XmlAttribute
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@XmlElement(name = "title")
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@XmlTransient
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(Date date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Book [id=" + id + ", name=" + name + ", author=" + author + ", date=" + date + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.jaxb;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.xml.bind.annotation.adapters.XmlAdapter;
|
||||
|
||||
public class DateAdapter extends XmlAdapter<String, Date> {
|
||||
|
||||
private static final ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>() {
|
||||
|
||||
@Override
|
||||
protected DateFormat initialValue() {
|
||||
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public Date unmarshal(String v) throws Exception {
|
||||
return dateFormat.get().parse(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String marshal(Date v) throws Exception {
|
||||
return dateFormat.get().format(v);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.jaxb;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
|
||||
public class Main {
|
||||
public static void marshal() throws JAXBException, IOException {
|
||||
Book book = new Book();
|
||||
book.setId(1L);
|
||||
book.setName("Book1");
|
||||
book.setAuthor("Author1");
|
||||
book.setDate(new Date());
|
||||
|
||||
JAXBContext context = JAXBContext.newInstance(Book.class);
|
||||
Marshaller marshaller = context.createMarshaller();
|
||||
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
||||
marshaller.marshal(book, new File("./book.xml"));
|
||||
}
|
||||
|
||||
public static Book unMashal() throws JAXBException, IOException {
|
||||
JAXBContext context = JAXBContext.newInstance(Book.class);
|
||||
Unmarshaller unmarshaller = context.createUnmarshaller();
|
||||
Book book = (Book) unmarshaller.unmarshal(new FileReader("./book.xml"));
|
||||
return book;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws JAXBException, IOException {
|
||||
marshal();
|
||||
Book book = unMashal();
|
||||
System.out.println(book.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
|
||||
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema"
|
||||
jaxb:extensionBindingPrefixes="xjc">
|
||||
|
||||
<jaxb:globalBindings>
|
||||
<xjc:simple />
|
||||
<xjc:serializable uid="-1" />
|
||||
<jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime"
|
||||
parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime"
|
||||
printMethod="javax.xml.bind.DatatypeConverter.printDateTime" />
|
||||
</jaxb:globalBindings>
|
||||
</jaxb:bindings>
|
||||
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<layout class="ch.qos.logback.classic.PatternLayout">
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg %n</pattern>
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<logger name="com.baeldung.hazelcast" level="INFO" additivity="false">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</logger>
|
||||
|
||||
<root level="DEBUG">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.baeldung.com/jaxb/gen"
|
||||
xmlns:userns="http://www.baeldung.com/jaxb/gen" elementFormDefault="qualified">
|
||||
|
||||
<element name="userRequest" type="userns:UserRequest"></element>
|
||||
<element name="userResponse" type="userns:UserResponse"></element>
|
||||
|
||||
<complexType name="UserRequest">
|
||||
<sequence>
|
||||
<element name="id" type="int" />
|
||||
<element name="name" type="string" />
|
||||
</sequence>
|
||||
</complexType>
|
||||
|
||||
<complexType name="UserResponse">
|
||||
<sequence>
|
||||
<element name="id" type="int" />
|
||||
<element name="name" type="string" />
|
||||
<element name="gender" type="string" />
|
||||
<element name="created" type="dateTime" />
|
||||
</sequence>
|
||||
</complexType>
|
||||
</schema>
|
||||
Reference in New Issue
Block a user