axis部署WebServices 和 JAXB的运用 (二)

 
三、 JAXB 的运用:
1 新建一个 xml schema 文件 po.xsd
< xsd:schema  xmlns:xsd ="http://www.w3.org/2001/XMLSchema" >
< xsd:element  name ="purchaseOrder"  type ="PurchaseOrderType" />
< xsd:element  name ="comment"  type ="xsd:string" />
< xsd:complexType  name ="PurchaseOrderType" >
  
< xsd:sequence >
    
< xsd:element  name ="shipTo"  type ="USAddress" />
    
< xsd:element  name ="billTo"  type ="USAddress" />
    
< xsd:element  ref ="comment"  minOccurs ="0" />
    
< xsd:element  name ="items"  type ="Items" />
  
</ xsd:sequence >
  
< xsd:attribute  name ="orderDate"  type ="xsd:date" />
</ xsd:complexType >

< xsd:complexType  name ="USAddress" >
  
< xsd:sequence >
    
< xsd:element  name ="name"  type ="xsd:string" />
    
< xsd:element  name ="street"  type ="xsd:string" />
    
< xsd:element  name ="city"  type ="xsd:string" />
    
< xsd:element  name ="state"  type ="xsd:string" />
    
< xsd:element  name ="zip"  type ="xsd:decimal" />
  
</ xsd:sequence >
  
< xsd:attribute  name ="country"  type ="xsd:NMTOKEN"  fixed ="US" />
</ xsd:complexType >

< xsd:complexType  name ="Items" >
  
< xsd:sequence >
    
< xsd:element  name ="item"  minOccurs ="1"  maxOccurs ="unbounded" >
      
< xsd:complexType >
        
< xsd:sequence >
         
< xsd:element  name ="productName"  type ="xsd:string" />
         
< xsd:element  name ="quantity" >
           
< xsd:simpleType >
             
< xsd:restriction  base ="xsd:positiveInteger" >
               
< xsd:maxExclusive  value ="100" />
             
</ xsd:restriction >
           
</ xsd:simpleType >
         
</ xsd:element >
         
< xsd:element  name ="USPrice"  type ="xsd:decimal" />
         
< xsd:element  ref ="comment"  minOccurs ="0" />
         
< xsd:element  name ="shipDate"  type ="xsd:date"  minOccurs ="0" />
        
</ xsd:sequence >
        
< xsd:attribute  name ="partNum"  type ="SKU"  use ="required" />
      
</ xsd:complexType >
    
</ xsd:element >
  
</ xsd:sequence >
</ xsd:complexType >

<!--  Stock Keeping Unit, a code for identifying products  -->
< xsd:simpleType  name ="SKU" >
  
< xsd:restriction  base ="xsd:string" >
     
< xsd:pattern  value ="d{3}-[A-Z]{2}" />
  
</ xsd:restriction >
</ xsd:simpleType >
</ xsd:schema >
 

2生成XML Schema 文件对应的类:

   1)在环境变量的系统变量的PATH中加入 <jwsdp安装目录>/jaxb/bin,(我的是C:/jwsdp-2.0/jaxb/bin; 然后以命令行的形式运行:

…./xjc  po.xsd -d src -p epri.jaxb

     其中  

      ·po.xsd Schema的文件名;

      ·-d  的选项,是指定系统生成的Java源代码所放置的目录;

      ·-p  的选项,是指定系统生成的Java源代码所在的Java Package的名称。

注:-d -p 都可以不写,直接运行: xjc po.xsd                 (不建议)

 

   例如:我们将po.xsd 放于C盘根目录下:

 

 

可以看到生成了两个java文件,它们就是po.xsd对应的实体类。

 

2)编写一个ant文件,用于在工程中直接生成XML Schema 文件对就的类,如下:

 

<? xml version="1.0" ?>
< project  basedir ="."  default ="compile" >
 
<!-- 这里是jwsdp的安装目录  -->
    
< property  name ="jwsdp.home"  value ="C:jwsdp-2.0"   />
    
< path  id ="classpath" >
        
< pathelement  path ="build"   />
        
< fileset  dir ="${jwsdp.home}"  includes ="jaxb/lib/*.jar"   />
        
< fileset  dir ="${jwsdp.home}"  includes ="jwsdp-shared/lib/*.jar"   />
        
< fileset  dir ="${jwsdp.home}"  includes ="jaxp/lib/**/*.jar"   />
    
</ path >
    
< taskdef  name ="xjc"  classname ="com.sun.tools.xjc.XJCTask" >
        
< classpath  refid ="classpath"   />
    
</ taskdef >
    
<!--  compile Java source files  -->
    
< target  name ="compile" >
        
<!--  generate the Java content classes from the schema  -->
        
< echo  message ="Compiling the schema external binding file..."   />
        
< xjc  schema ="po.xsd"  package ="primer.po"  target ="src"   />
        
<!--  compile all of the java sources  -->
        
< echo  message ="Compiling the java source files..."   />
    
</ target >
</ project >

Run As ->  Ant Build   XML Schema就可以生成对应的实体类,当然po.sxd build.xml都应位于工程的根目录下!

3写一个测试的java文件

Main.java:

import  java.io.FileOutputStream;
import  java.io.IOException;
import  java.math.BigDecimal;
import  java.math.BigInteger;
import  java.util.GregorianCalendar;
import  java.util.List;

import  javax.xml.bind.JAXBContext;
import  javax.xml.bind.JAXBElement;
import  javax.xml.bind.JAXBException;
import  javax.xml.bind.Marshaller;

import  javax.xml.bind. * ;
import  javax.xml.datatype.DatatypeFactory;
import  javax.xml.datatype.XMLGregorianCalendar;
import  javax.xml.datatype.DatatypeConfigurationException;
import  primer.po. * ;
public   class  Main {
    
public static void main( String[] args ) {
        
try {
            JAXBContext jc 
= JAXBContext.newInstance( "primer.po" );
            PurchaseOrderType po 
= new PurchaseOrderType();
            po.setOrderDate( getDate() );
            USAddress shipTo 
= createUSAddress( "Alice Smith",
                                                
"123 Maple Street",
                                                
"Cambridge",
                                                
"MA",
                                                
"12345" );
            po.setShipTo( shipTo );
            USAddress billTo 
= createUSAddress( "Robert Smith",
                                                
"8 Oak Avenue",
                                                
"Cambridge",
                                                
"MA",
                                                
"12345" );
            po.setBillTo( billTo );
            Items items 
= new Items();
            List
<Items.Item> itemList = items.getItem();
            
            
// start adding ItemType objects into it
            itemList.add( createItem( "Nosferatu - Special Edition (1929)"
                                      
new BigInteger( "5" ), 
                                      
new BigDecimal( "19.99" ), 
                                      
null,
                                      
null,
                                      
"242-NO" ) );
            itemList.add( createItem( 
"The Mummy (1959)"
                                      
new BigInteger( "3" ), 
                                      
new BigDecimal( "19.98" ), 
                                      
null,
                                      
null,
                                      
"242-MU" ) );
            itemList.add( createItem( 
"Godzilla and Mothra: Battle for Earth/Godzilla vs. King Ghidora"
                                      
new BigInteger( "3" ), 
                                      
new BigDecimal( "27.95" ), 
                                      
null,
                                      
null,
                                      
"242-GZ" ) );
            po.setItems( items );
        JAXBElement
<PurchaseOrderType> poElement = (new ObjectFactory()).createPurchaseOrder(po);
            Marshaller m 
= jc.createMarshaller();
            m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
            
            m.marshal(poElement, 
new FileOutputStream("test.xml"));
            
            m.marshal(poElement, System.out);
            
        }
 catch( JAXBException je ){
            je.printStackTrace();
        }
 catch( IOException ioe ) {
            ioe.printStackTrace();
        }

    }

    
public static USAddress createUSAddress(  String name, String street,
                                              String city, String state,
                                              String zip ) 
{
        USAddress address 
= new USAddress();
        address.setName( name );
        address.setStreet( street );
        address.setCity( city );
        address.setState( state );
        address.setZip( 
new BigDecimal( zip ) );
        
return address;
    }

    
public static Items.Item createItem( String productName,
                                         BigInteger quantity,
                                         BigDecimal price,
                                         String comment,
                                         XMLGregorianCalendar shipDate,
                                         String partNum ) 
{
        Items.Item item 
= new Items.Item();
        item.setProductName( productName );
        item.setQuantity( quantity );
        item.setUSPrice( price );
        item.setComment( comment );
        item.setShipDate( shipDate );
        item.setPartNum( partNum );
        
return item;
}

    
private static XMLGregorianCalendar getDate() {
    
try {
        
return DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar());
    }
 catch (DatatypeConfigurationException e) {
        
throw new Error(e);
    }

    }

}

Java Application运行 SayHelloClient.java

输出结果:

axisweb工程根目录下生成了一个test.xml 的文件,内容和上面一样!

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值