浏览 1626 次
|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2007-09-15 关键字: JAXB,Exception
笔者安装的是Java Web Services Developer Pack是jwsdp-2_0-windows-i586.exe,
You can reach it at: 安装完之后,笔者写了一个Java Application和一个web project,借助Java Web Services Developer Pack工具包中的JAXB,操纵通过xjc工具生成的与xsd文件相对应的Java类,生成该java类的对象,来产生一个xml文件。 下面是笔者编写Java Application的过程。 环境:Window xp,MyEclipse6.0,JDK1.5,JWSDP2.0。 步骤如下: 1、在MyEclipse6.0里面New一个Java Project,命名为:testJABX。 2、导入JAXB的工具包。右键点击testJABX,然后选择Build Path-->Configure Build Path,然后选择libraries选项卡,然后Add External JARS,加入jwsdp-2.0\jaxb\lib这个包下面的jaxb1-impl.jar,jaxb-api.jar,jaxb-impl.jar,jaxb-xjc.jar这四个包。 3、在命令行进入..\jwsdp-2.0\jaxb\bin,然后用xjc +xsd文件名,生成Java类。 4、将生成的类导进你的Java Project,将这些类组织好。 生成的java文件有:Items.java,PurchaseOrderType,USAddress.java,ObjectFactory.java。笔者把它组织在primer.po这个包 下面。 关于xsd文件生成java文件的资料,读者可以自行上网找,网上大把这样的资料。 不知怎么回事,贴不上xsd文件。不过,也无关紧要的了 5、写主程序,测试一下。结果是在控制台输出一个xml文件。xml文件中的数据值,是与程序员设置Java对象值一一对应的。 主程序主要是给xsd生成的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 { // This sample application demonstrates how to construct value classes // and create a java content tree from scratch and marshal it // to XML data public static void main( String[] args ) { try { // create a JAXBContext JAXBContext jc = JAXBContext.newInstance( "primer.po" ); // create an empty PurchaseOrder PurchaseOrderType po = new PurchaseOrderType(); // set the required orderDate attribute po.setOrderDate( getDate() ); // create shipTo USAddress object USAddress shipTo = createUSAddress( "Alice Smith", "123 Maple Street", "Cambridge", "MA", "12345" ); // set the required shipTo address po.setShipTo( shipTo ); // create billTo USAddress object USAddress billTo = createUSAddress( "Robert Smith", "8 Oak Avenue", "Cambridge", "MA", "12345" ); // set the requred billTo address po.setBillTo( billTo ); // create an empty Items object Items items = new Items(); // get a reference to the ItemType list List // 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" ) ); // set the required Items list po.setItems( items ); // create an element for marshalling JAXBElement // create a Marshaller and marshal to System.out 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 ) { // create an empty USAddress objects USAddress address = new USAddress(); // set properties on it address.setName( name ); address.setStreet( street ); address.setCity( city ); address.setState( state ); address.setZip( new BigDecimal( zip ) ); // return it return address; } public static Items.Item createItem( String productName, BigInteger quantity, BigDecimal price, String comment, XMLGregorianCalendar shipDate, String partNum ) { // create an empty ItemType object Items.Item item = new Items.Item(); // set properties on it item.setProductName( productName ); item.setQuantity( quantity ); item.setUSPrice( price ); item.setComment( comment ); item.setShipDate( shipDate ); item.setPartNum( partNum ); // return it return item; } private static XMLGregorianCalendar getDate() { try { return DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar()); } catch (DatatypeConfigurationException e) { throw new Error(e); } } } 6、主程序写好之后,发现会报出一个错误: The type javax.xml.stream.XMLStreamWriter cannot be resolved. It is indirectly referenced from required .class files 这是因为在JDK1.5中,没有XMLStreamWriter这个类。 所以,我们需要导进包含XMLStreamWriter的jar包。这个包是jsr173_1.0_api.jar。读者可以自己去sun的网站上下载。 本文附件也加了这个jar包。 因为这个错误,花费了笔者半天的时间,汗。 7、运行主程序,结果出来了。 Well Done!! 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
| 返回顶楼 | |
|
时间:2008-04-19
不错
Binding Schema那块也一起讲讲就好了 |
|
| 返回顶楼 | |



