JAXB란?

자바6 이상을 사용하는 별다른JAR없이 라이브러리를 사용할수있다고한다.

 

1. JAXB란 무엇인가? JAXB의 설정과 튜토리얼

1. JAXB (Java Architecture for XML Binding)란?
  – XML 스키마 데이터를 JAVA CLASS 형태로 만들 수 있습니다. Marshalling(Serialization)
  – 반대로 Java Class를 XML 데이터로 만들 수 있습니다.
     즉, JAXB를 이용한 XML 직렬화 Unmarshalling(Deserialization)
2. 환경 구성
  
  – JAXB는 JDK 1.6 이상을 요구하고 있습니다.

3. JAXB의 Annotation의 구성

@XmlRootElement(namespace = “namespace”) : 최상위 요소를 정의합니다.

@XmlType(propOrder = { “field2”, “field1”,.. }) : XML 필드 구성을 정의합니다.

@XmlElement(name = “neuName”) : XML 필드 요소를 정의 합니다.


4. 소스 설정

– 우선적으로 책(Book)이란 Class를 만들어 보겠습니다.

package de.vogella.xml.jaxb.model;

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

@XmlRootElement(name = “book”)
// If you want you can define the order in which the fields are written
// Optional
@XmlType(propOrder = { “author”, “name”, “publisher”, “isbn” })
public class Book {

private String name;
private String author;
private String publisher;
private String isbn;

// If you like the variable name, e.g. “name”, you can easily change this
// name for your XML-Output:
@XmlElement(name = “title”)
public String getName() {
return name;
}

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

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getPublisher() {
return publisher;
}

public void setPublisher(String publisher) {
this.publisher = publisher;
}

public String getIsbn() {
return isbn;
}

public void setIsbn(String isbn) {
this.isbn = isbn;
}

}

– 책을 사기 위해서는 서점이 필요하므로 BookStore를 만들어 보겠습니다.

package de.vogella.xml.jaxb.model;

import java.util.ArrayList;

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

//This statement means that class “Bookstore.java” is the root-element of our example
@XmlRootElement(namespace = “de.vogella.xml.jaxb.model”)
public class Bookstore {

// XmLElementWrapper generates a wrapper element around XML representation
@XmlElementWrapper(name = “bookList”)
// XmlElement sets the name of the entities
@XmlElement(name = “book”)
private ArrayList<Book> bookList;
private String name;
private String location;

public void setBookList(ArrayList<Book> bookList) {
this.bookList = bookList;
}

public ArrayList<Book> getBooksList() {
return bookList;
}

public String getName() {
return name;
}

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

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}
}

즉, 서점의 요소에는 북이 포함되어 있다고 생각 하시면 되시겠습니다. (북 < 서점)
이제 이런 Book XML 관계도를 XML 파일로 출력하는 main 클래스를 만들어 보겠습니다.
package test;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import de.vogella.xml.jaxb.model.Book;
import de.vogella.xml.jaxb.model.Bookstore;

public class BookMain {

private static final String BOOKSTORE_XML = “./bookstore-jaxb.xml”;

public static void main(String[] args) throws JAXBException, IOException {

ArrayList<Book> bookList = new ArrayList<Book>();

// create books
Book book1 = new Book();
book1.setIsbn(“978-0060554736”);
book1.setName(“The Game”);
book1.setAuthor(“Neil Strauss”);
book1.setPublisher(“Harpercollins”);
bookList.add(book1);

Book book2 = new Book();
book2.setIsbn(“978-3832180577”);
book2.setName(“Feuchtgebiete”);
book2.setAuthor(“Charlotte Roche”);
book2.setPublisher(“Dumont Buchverlag”);
bookList.add(book2);

// create bookstore, assigning book
Bookstore bookstore = new Bookstore();
bookstore.setName(“Fraport Bookstore”);
bookstore.setLocation(“Frankfurt Airport”);
bookstore.setBookList(bookList);

// create JAXB context and instantiate marshaller
JAXBContext context = JAXBContext.newInstance(Bookstore.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

// Write to System.out
m.marshal(bookstore, System.out);

// Write to File
m.marshal(bookstore, new File(BOOKSTORE_XML));

// get variables from our xml file, created before
System.out.println();
System.out.println(“Output from our XML File: “);
Unmarshaller um = context.createUnmarshaller();
Bookstore bookstore2 = (Bookstore) um.unmarshal(new FileReader(BOOKSTORE_XML));
ArrayList<Book> list = bookstore2.getBooksList();
for (Book book : list) {
System.out.println(“Book: ” + book.getName() + ” from ”
+ book.getAuthor());
}
}
}

5. 실제 구동 화면

– 파일 및 디렉토리 구성도 입니다.


– 파일을 작성합니다.

– 자바 Main Class로 출력을 시켜 봅니다.


6. 참고 URL

출처 : http://choyeonseop.blogspot.kr/2013/02/1-jaxb-jaxb.html

22 Replies to “JAXB란?”

Leave a Reply

Your email address will not be published.