Programming/javaFX
[javaFX] JAXB (Java Architecture for XML Binding) - java 11
생각하는로뎅
2021. 2. 20. 20:27
반응형
1. JAXB 란
XML <-> Java 간의 마샬링과 언마샬링 기능을 수행한다.
2. java 11 에서는 아래와 같이 라이브러리들을 추가해주어야한다.
activation-1.1.1.jar
0.07MB
jaxb-api-2.3.1.jar
0.12MB
jaxb-core-2.3.0.1.jar
0.24MB
jaxb-impl-2.3.1.jar
1.05MB
3. 소스
1) CommonUtil.java
package application.common;
import java.io.File;
import java.io.IOException;
import java.util.prefs.Preferences;
import application.Main;
public class CommonUtil {
public final static String PREF_XML_KEY = "saveFile";
public final static String XML_PATH = System.getProperty("user.dir");
public final static String XML_NAME = "saveFile.xml";
/**
* XML 세이브 파일 반환
* @return file
* @throws IOException
*/
public static File getSaveFile() throws IOException {
Preferences prefs = Preferences.userNodeForPackage(Main.class);
String filePath = prefs.get(PREF_XML_KEY, null);
File file = null;
if (filePath != null) {
file = new File(filePath);
} else {
file = new File(XML_PATH, XML_NAME);
}
if (!file.exists()) {
file.createNewFile();
}
setSaveFilePath(file);
return file;
}
/**
* XML 세이브 파일 경로 저장
* @return file
*/
public static void setSaveFilePath(File file) {
Preferences prefs = Preferences.userNodeForPackage(Main.class);
if (file != null) {
prefs.put(PREF_XML_KEY, file.getPath());
} else {
prefs.remove(PREF_XML_KEY);
}
}
}
2) CategoryModel.java
package application.model;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="item")
@XmlAccessorType(XmlAccessType.FIELD)
public class CategoryModel {
public String title; // 카테고리 타이틀
public CategoryModel() {
}
public CategoryModel(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
3) CategoryListModel.java
package application.model;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* 카테고리 리스트 Model
* @author 임성진
*
*/
@XmlRootElement(name="cateoryList")
public class CategoryListModel {
private List<CategoryModel> categoryList;
@XmlElement(name="item")
public List<CategoryModel> getCategoryList() {
return categoryList;
}
public void setCategoryList(List<CategoryModel> categoryList) {
this.categoryList = categoryList;
}
}
4) JAVA를 XML 파일로 저장 코드
public void test() throws IOException {
// 저장
saveData(CommonUtil.getSaveFile());
// 불러오기
loadData(CommonUtil.getSaveFile());
}
/**
* 저장하기
* @param file
*/
public void saveData(File file) {
try {
JAXBContext context = JAXBContext
.newInstance(CategoryListModel.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// 저장할 모델
CategoryListModel model = new CategoryListModel();
ArrayList<CategoryModel> saveModelList = new ArrayList<CategoryModel>();
saveModelList.add(new CategoryModel("임성진"));
model.setCategoryList(saveModelList);
// 마샬링 후 XML을 파일에 저장한다.
m.marshal(model, file);
} catch (Exception e) {
System.out.print(e.getMessage());
}
}
/**
* 불러오기
* @param file
*/
public void loadData(File file) {
try {
JAXBContext context = JAXBContext
.newInstance(CategoryListModel.class);
Unmarshaller um = context.createUnmarshaller();
// 파일로부터 XML을 읽은 다음 역 마샬링한다.
CategoryListModel model = (CategoryListModel) um.unmarshal(file);
} catch (Exception e) { // 예외를 잡는다
System.out.print(e.getMessage());
}
}
5. 출력물 : 한글은 꺠졌지만, 역마살링을하면 한글이 꺠지지 않았다.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cateoryList>
<item>
<title>?꾩꽦吏?/title>
</item>
</cateoryList>
반응형