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 > <!-- StockKeepingUnit,acodeforidentifyingproducts --> < 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安装目录>jaxbbin,(我的是C:jwsdp-2.0jaxbbin;) 然后以命令行的形式运行: ….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 文件对就的类,如下:
<?
xmlversion="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 > <!-- compileJavasourcefiles --> < target name ="compile" > <!-- generatetheJavacontentclassesfromtheschema --> < echo message ="Compilingtheschemaexternalbindingfile..." /> < xjc schema ="po.xsd" package ="primer.po" target ="src" /> <!-- compileallofthejavasources --> < echo message ="Compilingthejavasourcefiles..." /> </ 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 { publicstaticvoidmain(String[]args){ try{ JAXBContextjc=JAXBContext.newInstance("primer.po"); PurchaSEOrderTypepo=newPurchaSEOrderType(); po.setOrderDate(getDate()); USAddressshipTo=createUSAddress("AliceSmith", "123MapleStreet", "Cambridge", "MA", "12345"); po.setShipTo(shipTo); USAddressbillTo=createUSAddress("RobertSmith", "8OakAvenue", "12345"); po.setBillTo(billTo); Itemsitems=newItems(); List<Items.Item>itemList=items.getItem(); //startaddingItemTypeobjectsintoit itemList.add(createItem("Nosferatu-SpecialEdition(1929)", newBigInteger("5"), newBigDecimal("19.99"), null, "242-NO")); itemList.add(createItem("TheMummy(1959)", newBigInteger("3"), newBigDecimal("19.98"), "242-MU")); itemList.add(createItem("GodzillaandMothra:BattleforEarth/Godzillavs.KingGhidora", newBigDecimal("27.95"), "242-GZ")); po.setItems(items); JAXBElement<PurchaSEOrderType>poElement=(newObjectFactory()).createPurchaSEOrder(po); Marshallerm=jc.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE); m.marshal(poElement,newFileOutputStream("test.xml")); m.marshal(poElement,System.out); }catch(JAXBExceptionje){ je.printStackTrace(); }catch(IOExceptionioe){ ioe.printStackTrace(); } } publicstaticUSAddresscreateUSAddress(Stringname,Stringstreet, Stringcity,Stringstate, Stringzip){ USAddressaddress=newUSAddress(); address.setName(name); address.setStreet(street); address.setCity(city); address.setState(state); address.setZip(newBigDecimal(zip)); returnaddress; } publicstaticItems.ItemcreateItem(StringproductName, BigIntegerquantity, BigDecimalprice, Stringcomment, XMLGregorianCalendarshipDate, StringpartNum){ Items.Itemitem=newItems.Item(); item.setProductName(productName); item.setQuantity(quantity); item.setUSPrice(price); item.setComment(comment); item.setShipDate(shipDate); item.setPartNum(partNum); returnitem; } privatestaticXMLGregorianCalendargetDate(){ try{ returnDatatypeFactory.newInstance().newXMLGregorianCalendar(newGregorianCalendar()); }catch(DatatypeConfigurationExceptione){ thrownewError(e); } } }
用Java Application运行 SayHelloClient.java 输出结果:
在axisweb工程根目录下生成了一个test.xml 的文件,内容和上面一样! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |