The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
This section describes the Basic JAXB examples (Modify Marshal, Unmarshal Validate) that demonstrate how to:
The Modify Marshal example demonstrates how to modify a Java content tree.
import java.io.FileInputStream; import java.io.IOException; 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; import primer.po.*;
JAXBContext jc = JAXBContext.newInstance( "primer.po" );
Unmarshaller u = jc.createUnmarshaller(); PurchaseOrder po = (PurchaseOrder) u.unmarshal(new FileInputStream("po.xml"));
USAddress address = po.getBillTo(); address.setName("John Bob"); address.setStreet("242 Main Street"); address.setCity("Beverly Hills"); address.setState("CA"); address.setZip(new BigDecimal address.setName("John Bob"); address.setStreet("242 Main Street"); address.setCity("Beverly Hills"); address.setState("CA"); address.setZip(new BigDecimal("90210"));
Marshaller m = jc.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); m.marshal(po, System.out);
To compile and run the Modify Marshal example using Ant, in a terminal window, go to the jaxb-ri-install/samples/modify-marshal/ directory and type the following:
ant
The Unmarshal Validate example demonstrates how to enable validation during unmarshalling. Note that JAXB provides functions for validation during unmarshalling but not during marshalling. Validation is explained in more detail in More About Validation.
import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.UnmarshalException; import javax.xml.bind.Unmarshaller; import javax.xml.bind.ValidationEvent; import javax.xml.bind.ValidationEventHandler; import javax.xml.bind.ValidationEventLocator; import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Schema; import primer.po.*;
JAXBContext jc = JAXBContext.newInstance("primer.po");
Unmarshaller u = jc.createUnmarshaller();
u.setValidating( true );
PurchaseOrder po = (PurchaseOrder)u.unmarshal( new FileInputStream("po.xml"));
} catch( UnmarshalException ue ) { System.out.println("Caught UnmarshalException"); } catch( JAXBException je ) { je.printStackTrace(); } catch( IOException ioe ) { ioe.printStackTrace(); }
To compile and run the Unmarshal Validate example using Ant, in a terminal window, go to the jaxb-ri-install/samples/unmarshal-validate/ directory and type the following:
ant