jaxb学习

第一步:下载jwsdp-2_0-windows-i586.exe并安装,下载地址是sun的官网。

 

第二步:设置环境变量

          JWSDP_HOME=C:/jwsdp-2.0
    JWSDP_JAXB_LIB=%JWSDP_HOME%/jaxb/lib
    JWSDP_SHARED_LIB=%JWSDP_HOME%/jwsdp-shared/lib
    JWSDP_SJSXP_LIB=%JWSDP_HOME%/sjsxp/lib

 

           CLASSPATH=%JWSDP_JAXB_LIB%/jaxb-api.jar;%JWSDP_JAXB_LIB%/jaxb-impl.jar;%JWSDP_JAXB_LIB%/jaxb1-impl.jar;%JWSDP_JAXB_LIB%/jaxb-xjc.jar;%JWSDP_SJSXP_LIB%/sjsxp.jar;%JWSDP_SJSXP_LIB%/jsr173_api.jar;%JWSDP_SHARED_LIB%/activation.jar;%JWSDP_SHARED_LIB%/resolver.jar;%CLASSPATH%

 

    PATH=%JWSDP_HOME%/jaxb/bin;%JWSDP_HOME%/jwsdp-shared/bin;%PATH%

第三步:在工程src目录下建bookxsd.xsd 与 books.xml,也可以在网上下载

 

 输出:Learning JAXB
Java Webservices today and Beyond

 

 

注意:环境变量要设置正确,刚生成的Java类有错误,要引入jaxb下的jar包

     

 books.xml-------------------------------------

   <?xml version="1.0" ?> 
<Collection>
<books>
 <book itemId="999">
  <name>Learning JAXB</name>
  <ISBN>123445</ISBN>
  <price>34 $</price> 
<authors>
  <authorName>Jane Doe</authorName>
  </authors>
  <description>This books contains step by step instructions for beginners so that they can start using Java API for XML Binding.</description> 
<promotion>
  <Discount>10% on this book if purchased by March 2003</Discount>
  </promotion>
  <publicationDate>2003-01-01</publicationDate>
  <bookCategory>other</bookCategory>
  </book>
 <book itemId="129">
  <name>Java Webservices today and Beyond</name>
  <ISBN>522965</ISBN>
  <price>29 $</price> 
<authors>
  <authorName>John Brown</authorName>
  <authorName>Peter T.</authorName>
  </authors>
  <description>This books contains information for users so that they can start using Java Web Services Developer Pack.</description> 
<promotion>
  <Discount>Buy one get Learning webservices Part 1 free</Discount>
  </promotion>
  <publicationDate>2002-11-01</publicationDate>
  <bookCategory>magazine</bookCategory>
  </book>
  </books>
  </Collection>
  
bookxsd.xsd------------------------------- 
<?xml version="1.0" ?> 
<xs:schema xmlns:xs="
http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="1.0">
 <xs:element name="Collection">
 <xs:complexType>
 <xs:sequence>
 <xs:element name="books">
 <xs:complexType>
 <xs:sequence>
  <xs:element name="book" type="bookType" minOccurs="1" maxOccurs="unbounded" />
  </xs:sequence>
  </xs:complexType>
  </xs:element>
  </xs:sequence>
  </xs:complexType>
  </xs:element>
 <xs:complexType name="bookType">
 <xs:sequence>
  <xs:element name="name" type="xs:string" />
  <xs:element name="ISBN" type="xs:long" />
  <xs:element name="price" type="xs:string" /> 
<xs:element name="authors">
 <xs:complexType>
<xs:sequence>
  <xs:element name="authorName" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
  </xs:sequence>
  </xs:complexType>
  </xs:element>
  <xs:element name="description" type="xs:string" minOccurs="0" /> 
<xs:element name="promotion">
 <xs:complexType>
 <xs:choice>
  <xs:element name="Discount" type="xs:string" />
  <xs:element name="None" type="xs:string" />
  </xs:choice>
  </xs:complexType>
  </xs:element>
  <xs:element name="publicationDate" type="xs:date" /> 
<xs:element name="bookCategory">
 <xs:simpleType>
 <xs:restriction base="xs:NCName">
  <xs:enumeration value="magazine" />
  <xs:enumeration value="novel" />
  <xs:enumeration value="fiction" />
  <xs:enumeration value="other" />
  </xs:restriction>
  </xs:simpleType>
  </xs:element>
  </xs:sequence>
  <xs:attribute name="itemId" type="xs:string" />
  </xs:complexType>
<xs:simpleType name="bookCategoryType">
 <xs:restriction base="xs:string">
  <xs:enumeration value="magazine" />
  <xs:enumeration value="novel" />
  <xs:enumeration value="fiction" />
  <xs:enumeration value="other" />
  </xs:restriction>
  </xs:simpleType>
  </xs:schema>

 

第四步:在工程的根目录下执行命令
    D:/workspace/JAXBDemo>xjc -p com.topsoft.jaxb -d src src/books.xsd
    生成4个java文件:
 com/ce/BookCategoryType.java
 com/ce/BookType.java
 com/ce/Collection.java
 com/ce/ObjectFactory.java

 

第五步:

编写测试类

import java.io.File;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import com.topsoft.jaxp.Collection.Books;

public class JAXBTest {

 public static void main(String[] args) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance("com.topsoft.jaxp");
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    Collection collection= (Collection)unmarshaller.unmarshal(new File( "src/books.xml"));
    Books books = collection.getBooks();
    List<BookType> bookList = books.getBook();
    for (BookType bookType : bookList) {
     System.out.println(bookType.getName());
    }
   }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值