1 / 9

JAXB (Java Architecture for XML Binding)

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 을 매핑할 수 있도록 함 중요 기능

kali
Download Presentation

JAXB (Java Architecture for XML Binding)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JAXB(Java Architecture for XML Binding) Internet Computing Laboratory @ KUT (http://icl.kut.ac.kr) Youn-Hee Han

  2. 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.

  3. 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>

  4. 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) { … } }

  5. 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>

  6. 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); } } }

  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. 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); } } }

  9. Marshalling (Java Object  XML) • Marshalling 예제 컴파일 • D:/JAXBExample 폴더에서 다음 명령 실행 • javac –d . kut/ime/jaxb/JAXBMarshal.java • 수행결과 모습 • 실행 • D:/JAXBExample 폴더에서 다음 명령 실행 • java kut.ime.jaxb.JAXBMarshal • 수행결과 • 같은폴더에 birthdateInstance2.xml 가 생성되어 있음을 확인 • 파일 내용 확인

More Related