三、
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
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
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
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
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
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<!--
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
>
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;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
javax.xml.bind.JAXBContext;
import
javax.xml.bind.JAXBElement;
import
javax.xml.bind.JAXBException;
import
javax.xml.bind.Marshaller;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
import
javax.xml.bind.
*
;
import
javax.xml.datatype.DatatypeFactory;
import
javax.xml.datatype.XMLGregorianCalendar;
import
javax.xml.datatype.DatatypeConfigurationException;
import
primer.po.
*
;
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
public
class
Main
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
public static void main( String[] args ) {
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
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);
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch( JAXBException je ){
je.printStackTrace();
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch( IOException ioe ) {
ioe.printStackTrace();
}
}
public static USAddress createUSAddress( String name, String street,
String city, String state,
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
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,
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
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;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
private static XMLGregorianCalendar getDate() {
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
try {
return DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar());
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
} catch (DatatypeConfigurationException e) {
throw new Error(e);
}
}
}
用Java Application运行 SayHelloClient.java
输出结果:
在axisweb工程根目录下生成了一个test.xml 的文件,内容和上面一样!