test.xsd html
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="expenseReport" type="ExpenseT" /> <xs:complexType name="ExpenseT"> <xs:sequence> <xs:element name="user" type="UserT"/> <xs:element name="items" type="ItemListT"/> </xs:sequence> </xs:complexType> <xs:complexType name="UserT"> <xs:sequence> <xs:element name="userName" type="xs:string" /> </xs:sequence> </xs:complexType> <xs:complexType name="ItemListT"> <xs:sequence> <xs:element name="item" type="ItemT" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> <xs:complexType name="ItemT"> <xs:sequence> <xs:element name="itemName" type="xs:string" /> <xs:element name="purchasedOn" type="xs:string" /> <xs:element name="amount" type="xs:decimal" /> </xs:sequence> </xs:complexType> </xs:schema>
xjc test.xsd java
C:\work\java\jaxb\xsdtest>xjc test.xsd
parsing a schema...
compiling a schema...
generated\ExpenseT.java
generated\ItemListT.java
generated\ItemT.java
generated\ObjectFactory.java
generated\UserT.java ide
接下來一個簡單的例子 spa
package generated; import java.io.File; import java.math.BigDecimal; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; public class Main { public static void main(String[] args) throws JAXBException { ObjectFactory factory = new ObjectFactory(); // UserT user = factory.createUserT(); UserT user = new UserT(); user.setUserName("Sanaulla"); ItemT item = factory.createItemT(); item.setItemName("Seagate External HDD"); item.setPurchasedOn("August 24, 2010"); item.setAmount(new BigDecimal("6776.5")); ItemListT itemList = factory.createItemListT(); itemList.getItem().add(item); ExpenseT expense = factory.createExpenseT(); expense.setUser(user); expense.setItems(itemList); JAXBContext context = JAXBContext.newInstance("generated"); JAXBElement<ExpenseT> element = factory.createExpenseReport(expense); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty("jaxb.formatted.output",Boolean.TRUE); marshaller.marshal(element,System.out); //Unmarshal JAXBContext jc = JAXBContext.newInstance( "generated" ); Unmarshaller u = jc.createUnmarshaller(); JAXBElement<ExpenseT> o = (JAXBElement<ExpenseT>)u.unmarshal( new File( "UserT.xml" ) ); ExpenseT expenseT = o.getValue(); System.out.println(expenseT.getUser().getUserName()); } }
在上例咱們能夠直接new而不一樣ObjectFactory, 可是在很複雜的狀況下,ObjectFactory能夠帶來很大的便利,仍是用StackOverflow來回答吧, code
http://stackoverflow.com/questions/953723/whats-the-point-of-jaxb-2s-objectfactory-classes orm
Backward compatibility isn't the only reason. :-P xml
With more complicated schemas, such as ones that have complicated constraints on the values that an element's contents can take on, sometimes you need to create actual JAXBElement objects. They are not usually trivial to create by hand, so the create* methods do the hard work for you. Example (from the XHTML 1.1 schema): @XmlElementDecl(namespace = "http://www.w3.org/1999/xhtml", name = "style", scope = XhtmlHeadType.class) public JAXBElement<XhtmlStyleType> createXhtmlHeadTypeStyle(XhtmlStyleType value) { return new JAXBElement<XhtmlStyleType>(_XhtmlHeadTypeStyle_QNAME, XhtmlStyleType.class, XhtmlHeadType.class, value); } This is how you get a <style> tag into a <head> tag: ObjectFactory factory = new ObjectFactory(); XhtmlHtmlType html = factory.createXhtmlHtmlType(); XhtmlHeadType head = factory.createXhtmlHeadType(); html.setHead(head); XhtmlStyleType style = factory.createXhtmlStyleType(); head.getContent().add(factory.createXhtmlHeadTypeStyle(style)); The first three uses of the ObjectFactory could be considered superfluous (though useful for consistency), but the fourth one makes JAXB much, much easier to use. Imaging having to write a new JAXBElement out by hand each time!
run一下 htm
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <expenseReport> <user> <userName>Sanaulla</userName> </user> <items> <item> <itemName>Seagate External HDD</itemName> <purchasedOn>August 24, 2010</purchasedOn> <amount>6776.5</amount> </item> </items> </expenseReport>