Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Pro XML Development with Java Technology 2006 phần 6 pptx
Nội dung xem thử
Mô tả chi tiết
CHAPTER 6 ■ OBJECT BINDING WITH JAXB 171
Binding Catalog Schema to Java Classes
In this section, you will bind the catalog schema shown in Listing 6-6 to its Java content classes.
You’ll subsequently use the Java content classes to marshal and unmarshal the XML document
shown in Listing 6-7. You will run xjc from within Eclipse. Therefore, configure xjc as an external
tool in Eclipse, similar to the JAXB 1.0 project configuration. The only difference for the JAXB 2.0
project is that the xjc batch file Location field is set to the JAXB 2.0 xjc batch file. You set the environment variables JAVA_HOME and JAXB_HOME similar to JAXB 1.0. Set JAXB_HOME for Chapter6-JAXB2.0
to C:\Sun\jwsdp-2.0\jaxb. To add the xjc configuration to the External Tools menu, select the
Common tab, and select the check box External Tools in the Display in Favorites menu area, as
shown in Figure 6-7.
To run the xjc compiler on the example schema, catalog.xsd, select the catalog.xsd file in the
Package Explorer and then select Run ➤ External Tools ➤ XJC. Schema-derived classes get generated in
the gen_source folder, as shown in Figure 6-12.
Figure 6-12. Schema-derived Java content classes generated by xjc
Java classes and interfaces are generated in the package generated by default. Fewer classes are
generated with JAXB 2.0 than with JAXB 1.0. For each xsd:complexType schema component, one
value class gets generated, instead of an interface and an implementation class. For example, for the
complex type catalogType, shown in Listing 6-33, the value class CatalogType.java gets generated.
Listing 6-33. The Complex Type catalogType
<xsd:complexType name="catalogType">
<xsd:sequence>
<xsd:element ref="journal" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="section" type="xsd:string"/>
<xsd:attribute name="publisher" type="xsd:string"/>
</xsd:complexType>
Vohra_706-0C06.fm Page 171 Thursday, July 13, 2006 1:11 PM
172 CHAPTER 6 ■ OBJECT BINDING WITH JAXB
The CatalogType.java class consists of getter and setter methods for each of the attributes of
the catalogType complex type. A getter method for the complex type journalType with the return
type List<JournalType> also gets generated. Listing 6-34 shows CatalogType.java.
Listing 6-34. CatalogType.java
package generated;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.AccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
import generated.CatalogType;
import generated.JournalType;
@XmlAccessorType(AccessType.FIELD)
@XmlType(name = "catalogType", propOrder = {
"journal"
})
public class CatalogType {
protected List<JournalType> journal;
@XmlAttribute
protected String publisher;
@XmlAttribute
protected String section;
public List<JournalType> getJournal() {
if (journal == null) {
journal = new ArrayList<JournalType>();
}
return this.journal;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String value) {
this.publisher = value;
}
public String getSection() {
return section;
}
Vohra_706-0C06.fm Page 172 Thursday, July 13, 2006 1:11 PM
CHAPTER 6 ■ OBJECT BINDING WITH JAXB 173
public void setSection(String value) {
this.section = value;
}
}
Similarly, the value class JournalType.java gets generated for the complex type journalType,
and the value class ArticleType.java gets generated for the complex type articleType. An
ObjectFactory.java factory class gets generated, which consists of the create methods for each of
the complex type and element declarations in the example schema. For example, the ObjectFactory
class method for the complex type catalogType is createCatalogType(), and its return type is
CatalogType. The ObjectFactory class method for the element catalog is
createCatalog(CatalogType), and its return type is JAXBElement<CatalogType>. Listing 6-35 shows
ObjectFactory.java.
Listing 6-35. ObjectFactory.java
package generated;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;
import generated.ArticleType;
import generated.CatalogType;
import generated.JournalType;
import generated.ObjectFactory;
@XmlRegistry
public class ObjectFactory {
private final static QName _Article_QNAME = new QName("", "article");
private final static QName _Journal_QNAME = new QName("", "journal");
private final static QName _Catalog_QNAME = new QName("", "catalog");
public ObjectFactory() {
}
public JournalType createJournalType() {
return new JournalType();
}
public ArticleType createArticleType() {
return new ArticleType();
}
public CatalogType createCatalogType() {
return new CatalogType();
}
@XmlElementDecl(namespace = "", name = "article")
public JAXBElement<ArticleType> createArticle(ArticleType value) {
return new JAXBElement<ArticleType>
(_Article_QNAME, ArticleType.class, null, value);
}
Vohra_706-0C06.fm Page 173 Thursday, July 13, 2006 1:11 PM