Download presentation
Presentation is loading. Please wait.
Published byVictor Harris Modified over 8 years ago
1
JAXB (Java Architecture for XML Binding) Internet Computing Laboratory @ KUT (http://icl.kut.ac.kr) Youn-Hee Han
2
2 What is JAXB? JAXB (Java Architecture for XML Binding) SAX 와 DOM 을 대체하는 기술 Java 프로그래머로 하여금 Java Class 와 XML 을 매핑할 수 있도록 함 중요 기능 Convert XML Schema into Class Representation Marshal Java objects into XML Unmarshal XML back into Java objects.
3
3 Convert XML Schema into Class Representation XML Schema 준비 XML 바인딩 컴파일러 – xjc 컴파일러 xjc –p kut.ime.jaxb –d. birthdate.xsd D:/JAXBExample/birthdate.xsd
4
4 Convert XML Schema into Class Representation 생성된 Birthdate.java 파일의 주 내용 package kut.ime.jaxb; public class Birthdate { protected String month; protected int day; protected int year; String getMonth() { … } void setMonth(String value) { … } int getDay() { … } void setDay(int value) { … } int getYear() { … } void setYear(int value) { … } } D:/JAXBExample/kut/ime/jaxb/birthdate.java
5
5 Unmarshalling (XML Java Object) 기존 XML Schema 의 인스턴스 xml 문서 준비 January 1 1900 D:/JAXBExample/birthdateInstance.xml
6
6 Unmarshalling (XML Java Object) Unmarshalling 예제 package kut.ime.jaxb; import javax.xml.bind.*; import java.io.File; public class JAXBUnmarshal { public static void main(String[] args) { try { JAXBContext jc = JAXBContext.newInstance("kut.ime.jaxb"); Unmarshaller unmarshaller = jc.createUnmarshaller(); Birthdate instance = (Birthdate)unmarshaller.unmarshal(new File("birthdateInstance.xml")); System.out.println("Month is: " + instance.getMonth()); System.out.println("Day is: " + instance.getDay()); System.out.println("Year is: " + instance.getYear()); } catch (Exception e) { System.err.println(e); } D:/JAXBExample/kut/ime/jaxb/ JAXBUnmarshal.java
7
7 Unmarshalling (XML Java Object) Unmarshalling 예제 컴파일 D:/JAXBExample 폴더에서 다음 명령 실행 javac –d. kut/ime/jaxb/JAXBUnmarshal.java javac –d. kut/ime/jaxb/ObjectFactory.java 수행 결과 모습 실행 D:/JAXBExample 폴더에서 다음 명령 실행 java kut.ime.jaxb.JAXBUnmarshal 수행 결과 모습
8
8 Marshalling (Java Object XML) Marshalling 예제 package kut.ime.jaxb; import javax.xml.bind.*; import java.io.File; import java.io.FileOutputStream; public class JAXBMarshal { public static void main(String[] args) { try { ObjectFactory objFactory = new ObjectFactory(); Birthdate birthdate = objFactory.createBirthdate(); birthdate.setMonth("January"); birthdate.setDay(1); birthdate.setYear(1900); JAXBContext jaxbContext = JAXBContext.newInstance("kut.ime.jaxb"); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true)); marshaller.marshal(birthdate, new FileOutputStream("birthdateInstance2.xml")); } catch (Exception e) { System.err.println(e); } D:/JAXBExample/kut/ime/jaxb/JAXBMarshal.java
9
9 Marshalling 예제 컴파일 D:/JAXBExample 폴더에서 다음 명령 실행 javac –d. kut/ime/jaxb/JAXBMarshal.java 수행 결과 모습 실행 D:/JAXBExample 폴더에서 다음 명령 실행 java kut.ime.jaxb.JAXBMarshal 수행 결과 같은 폴더에 birthdateInstance2.xml 가 생성되어 있음을 확인 파일 내용 확인 Marshalling (Java Object XML)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.