90 likes | 226 Vues
JAXB (Java Architecture for XML Binding) provides a convenient way for Java developers to map XML schemas to Java classes and vice versa. This technology can replace SAX and DOM, enabling seamless data conversion between object representations and XML format. Important features include the ability to convert XML schemas into class representations, marshal Java objects into XML, and unmarshal XML back into Java objects. This guide details the process of using JAXB, including example code for converting objects and marshalling/unmarshalling XML data.
E N D
JAXB(Java Architecture for XML Binding) Internet Computing Laboratory @ KUT (http://icl.kut.ac.kr) Youn-Hee Han
What is JAXB? • JAXB (Java Architecture for XML Binding) • SAX와 DOM을 대체하는 기술 • Java 프로그래머로 하여금 Java Class와 XML 을 매핑할 수 있도록 함 • 중요 기능 • Convert XML Schemainto Class Representation • Marshal Java objects into XML • Unmarshal XML back into Java objects.
Convert XML Schema into Class Representation • XML Schema 준비 • XML 바인딩 컴파일러 – xjc 컴파일러 • xjc –p kut.ime.jaxb –d . birthdate.xsd D:/JAXBExample/birthdate.xsd <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="birthdate"> <xs:complexType> <xs:sequence> <xs:element name="month" type="xs:string" /> <xs:element name="day" type="xs:int" /> <xs:element name="year" type="xs:int" /> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Convert XML Schema into Class Representation • 생성된 Birthdate.java 파일의 주 내용 D:/JAXBExample/kut/ime/jaxb/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) { … } }
Unmarshalling (XML Java Object) • 기존 XML Schema의 인스턴스 xml 문서 준비 D:/JAXBExample/birthdateInstance.xml <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <birthdate> <month>January</month> <day>1</day> <year>1900</year> </birthdate>
Unmarshalling (XML Java Object) • Unmarshalling 예제 D:/JAXBExample/kut/ime/jaxb/ JAXBUnmarshal.java 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); } } }
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 • 수행결과 모습
Marshalling (Java Object XML) • Marshalling 예제 D:/JAXBExample/kut/ime/jaxb/JAXBMarshal.java 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); } } }
Marshalling (Java Object XML) • Marshalling 예제 컴파일 • D:/JAXBExample 폴더에서 다음 명령 실행 • javac –d . kut/ime/jaxb/JAXBMarshal.java • 수행결과 모습 • 실행 • D:/JAXBExample 폴더에서 다음 명령 실행 • java kut.ime.jaxb.JAXBMarshal • 수행결과 • 같은폴더에 birthdateInstance2.xml 가 생성되어 있음을 확인 • 파일 내용 확인