프로그래밍/웹서비스
JavaTM Web Services Developer Pack 2.0 (3)
라제폰
2008. 12. 13. 20:28
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> |