2011년 5월 24일 화요일

JavaTM Web Services Developer Pack 2.0 (3)

JAXB 2.0을 이용한 Java-to-XML Schema
JAXB 1.x에서는 Schema-to-Java 만 지원되었었다. 여기서는 JAXB 2.0에서 추가된 Java-to-Schema에 관하여 살펴보도록 한다.

Sample Java Class

techtip.Scientist.java
package techtip;
import javax.xml.bind.annotation.*;
import java.util.Collection;
import techtip.Person;
@XmlRootElement
public class Scientist {
private Person person;
private String researchInstitute;
private Collection publications;
public Scientist() {
}
public Scientist (Person p, String ri, Collection col) {
person = p;
researchInstitute = ri;
publications = col;
}
public Person getPerson() {
return person;
}
public void setPerson (Person value) {
person = value;
}
public String getresearchInstitute() {
return researchInstitute;
}
public void setResearchInstitute(String institute) {
researchInstitute = institute;
}
public Collection getPublications() {
return publications;
}
public void setPublications(Collection publish) {
publications = publish;
}
}


techtip.Person.java
package techtip;
import javax.xml.bind.annotation.*;
import techtip.*;
@XmlType
public class Person {
private String name;
private int age;
private String sex;
public Person() {
}
public Person(String name, int age, String sex){
this.name=name;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String value) {
name = value;
}
public int getAge() {
return age;
}
public void setAge(int value) {
age = value;
}
public String getSex() {
return sex;
}
public void setSex(String value) {
sex = value;
}
}


@XmlRootElement : 해당 클래스는 Top 레벨 클래스임을 의미하는 Annotation
@Xmltype : 해당 클래스가 구조체임을 의미하는 Annotation

XmlSchema Generate

C:jaxbtechtipclasses>set path=%path%;C:jwsdp-2.0jaxbbin;
C:jaxbtechtipclasses>schemagen
Usage: schemagen [-options ...] <java files>
Options:
-d <path> : specify where to place processor and javac generated class files
-cp <path> : specify where to find user specified files
-classpath <path> : specify where to find user specified files
-version : display version information
C:jaxbtechtipclasses>schemagen techtip.Scientist
Note: Writing schema1.xsd


schema1.xsd
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="scientist" type="scientist"/>
<xs:complexType name="scientist">
<xs:sequence>
<xs:element name="person" type="person" minOccurs="0"/>
<xs:element name="publications" type="xs:anyType" nillable="true" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="person">
<xs:sequence>
<xs:element name="age" type="xs:int"/>
<xs:element name="name" type="xs:string" minOccurs="0"/>
<xs:element name="sex" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

댓글 없음:

댓글 쓰기

블록체인 개요 및 오픈소스 동향

블록체인(block chain) 블록체인은 공공 거래장부이며 가상 화폐로 거래할때 발생할때 발생할 수 있는 해킹을 막는 기술. 분산 데이터베이스의 한 형태로, 지속적으로 성장하는 데이터 기록 리스트로서 분산 노드의 운영자에 의한 임의 조작이 불가...