jaxbcontext.html

来自「j2ee帮助文档软件设计/软件工程 文件格式」· HTML 代码 · 共 989 行 · 第 1/4 页

HTML
989
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><!--NewPage--><HTML><HEAD><!-- Generated by javadoc (build 1.5.0_14) on Mon Jan 28 05:46:58 PST 2008 --><TITLE>JAXBContext (Java EE 5)</TITLE><META NAME="keywords" CONTENT="javax.xml.bind.JAXBContext class"><LINK REL ="stylesheet" TYPE="text/css" HREF="../../../stylesheet.css" TITLE="Style"><SCRIPT type="text/javascript">function windowTitle(){    parent.document.title="JAXBContext (Java EE 5)";}</SCRIPT><NOSCRIPT></NOSCRIPT></HEAD><BODY BGCOLOR="white" onload="windowTitle();"><!-- ========= START OF TOP NAVBAR ======= --><A NAME="navbar_top"><!-- --></A><A HREF="#skip-navbar_top" title="Skip navigation links"></A><TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0" SUMMARY=""><TR><TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1"><A NAME="navbar_top_firstrow"><!-- --></A><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3" SUMMARY="">  <TR ALIGN="center" VALIGN="top">  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> &nbsp;<FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A>&nbsp;</TD>  </TR></TABLE></TD><TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM></EM></TD></TR><TR><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">&nbsp;<A HREF="../../../javax/xml/bind/Element.html" title="interface in javax.xml.bind"><B>PREV CLASS</B></A>&nbsp;&nbsp;<A HREF="../../../javax/xml/bind/JAXBElement.html" title="class in javax.xml.bind"><B>NEXT CLASS</B></A></FONT></TD><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">  <A HREF="../../../index.html?javax/xml/bind/JAXBContext.html" target="_top"><B>FRAMES</B></A>  &nbsp;&nbsp;<A HREF="JAXBContext.html" target="_top"><B>NO FRAMES</B></A>  &nbsp;&nbsp;<SCRIPT type="text/javascript">  <!--  if(window==top) {    document.writeln('<A HREF="../../../allclasses-noframe.html"><B>All Classes</B></A>');  }  //--></SCRIPT><NOSCRIPT>  <A HREF="../../../allclasses-noframe.html"><B>All Classes</B></A></NOSCRIPT></FONT></TD></TR><TR><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">  SUMMARY:&nbsp;NESTED&nbsp;|&nbsp;<A HREF="#field_summary">FIELD</A>&nbsp;|&nbsp;<A HREF="#constructor_summary">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_summary">METHOD</A></FONT></TD><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">DETAIL:&nbsp;<A HREF="#field_detail">FIELD</A>&nbsp;|&nbsp;<A HREF="#constructor_detail">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_detail">METHOD</A></FONT></TD></TR></TABLE><A NAME="skip-navbar_top"></A><!-- ========= END OF TOP NAVBAR ========= --><HR><!-- ======== START OF CLASS DATA ======== --><H2><FONT SIZE="-1">javax.xml.bind</FONT><BR>Class JAXBContext</H2><PRE><A HREF="http://java.sun.com/j2se/1.5/docs/api/java/lang/Object.html" title="class or interface in java.lang">java.lang.Object</A>  <IMG SRC="../../../resources/inherit.gif" ALT="extended by "><B>javax.xml.bind.JAXBContext</B></PRE><HR><DL><DT><PRE>public abstract class <B>JAXBContext</B><DT>extends <A HREF="http://java.sun.com/j2se/1.5/docs/api/java/lang/Object.html" title="class or interface in java.lang">Object</A></DL></PRE><P><p> The <tt>JAXBContext</tt> class provides the client's entry point to the  JAXB API. It provides an abstraction for managing the XML/Java binding  information necessary to implement the JAXB binding framework operations:  unmarshal, marshal and validate. <p>A client application normally obtains new instances of this class using one of these two styles for newInstance methods, although there are other  specialized forms of the method available: <ul>   <li><A HREF="../../../javax/xml/bind/JAXBContext.html#newInstance(java.lang.String, java.lang.ClassLoader)"><CODE>JAXBContext.newInstance( "com.acme.foo:com.acme.bar" )</CODE></A> <br/>   The JAXBContext instance is initialized from a list of colon    separated Java package names. Each java package contains   JAXB mapped classes, schema-derived classes and/or user annotated    classes. Additionally, the java package may contain JAXB package annotations    that must be processed. (see JLS 3rd Edition, Section 7.4.1. Package Annotations).   </li>   <li><A HREF="../../../javax/xml/bind/JAXBContext.html#newInstance(java.lang.Class...)"><CODE>JAXBContext.newInstance( com.acme.foo.Foo.class )</CODE></A> <br/>    The JAXBContext instance is intialized with class(es)     passed as parameter(s) and classes that are statically reachable from     these class(es). See <A HREF="../../../javax/xml/bind/JAXBContext.html#newInstance(java.lang.Class...)"><CODE>newInstance(Class...)</CODE></A> for details.   </li> </ul> <p> <blockquote> <i><B>SPEC REQUIREMENT:</B> the provider must supply an implementation class containing the following method signatures:</i> <pre> public static JAXBContext createContext( String contextPath, ClassLoader classLoader, Map<String,Object> properties ) throws JAXBException public static JAXBContext createContext( Class[] classes, Map<String,Object> properties ) throws JAXBException </pre> <p><i> The following JAXB 1.0 requirement is only required for schema to  java interface/implementation binding. It does not apply to JAXB annotated classes. JAXB Providers must generate a <tt>jaxb.properties</tt> file in  each package containing schema derived classes.  The property file must  contain a property named <tt>javax.xml.bind.context.factory</tt> whose  value is the name of the class that implements the <tt>createContext</tt>  APIs.</i>  <p><i> The class supplied by the provider does not have to be assignable to  <tt>javax.xml.bind.JAXBContext</tt>, it simply has to provide a class that implements the <tt>createContext</tt> APIs.</i>  <p><i> In addition, the provider must call the  <A HREF="../../../javax/xml/bind/DatatypeConverter.html#setDatatypeConverter(javax.xml.bind.DatatypeConverterInterface)"><CODE>DatatypeConverter.setDatatypeConverter</CODE></A> api prior to any client  invocations of the marshal and unmarshal methods.  This is necessary to  configure the datatype converter that will be used during these operations.</i> </blockquote> <p> <a name="Unmarshalling"></a> <b>Unmarshalling</b> <p> <blockquote> The <A HREF="../../../javax/xml/bind/Unmarshaller.html" title="interface in javax.xml.bind"><CODE>Unmarshaller</CODE></A> class provides the client application the ability to convert XML data into a tree of Java content objects. The unmarshal method allows for  any global XML element declared in the schema to be unmarshalled as the root of an instance document. Additionally, the unmarshal method allows for an unrecognized root element that  has  an xsi:type attribute's value that references a type definition declared in  the schema  to be unmarshalled as the root of an instance document. The <tt>JAXBContext</tt> object  allows the merging of global elements and type definitions across a set of schemas (listed in the <tt>contextPath</tt>). Since each schema in the schema set can belong to distinct namespaces, the unification of schemas to an unmarshalling  context should be namespace independent.  This means that a client  application is able to unmarshal XML documents that are instances of any of the schemas listed in the <tt>contextPath</tt>.  For example: <pre>        JAXBContext jc = JAXBContext.newInstance( "com.acme.foo:com.acme.bar" );        Unmarshaller u = jc.createUnmarshaller();        FooObject fooObj = (FooObject)u.unmarshal( new File( "foo.xml" ) ); // ok        BarObject barObj = (BarObject)u.unmarshal( new File( "bar.xml" ) ); // ok        BazObject bazObj = (BazObject)u.unmarshal( new File( "baz.xml" ) ); // error, "com.acme.baz" not in contextPath </pre> <p> The client application may also generate Java content trees explicitly rather than unmarshalling existing XML data.  For all JAXB-annotated value classes, an application can create content using constructors.  For schema-derived interface/implementation classes and for the creation of elements that are not bound to a JAXB-annotated class, an application needs to have access and knowledge about each of  the schema derived <tt> ObjectFactory</tt> classes that exist in each of  java packages contained in the <tt>contextPath</tt>.  For each schema  derived java class, there is a static factory method that produces objects  of that type.  For example,  assume that after compiling a schema, you have a package <tt>com.acme.foo</tt>  that contains a schema derived interface named <tt>PurchaseOrder</tt>.  In  order to create objects of that type, the client application would use the  factory method like this: <pre>       com.acme.foo.PurchaseOrder po =            com.acme.foo.ObjectFactory.createPurchaseOrder(); </pre> <p> Once the client application has an instance of the the schema derived object, it can use the mutator methods to set content on it. <p> For more information on the generated <tt>ObjectFactory</tt> classes, see Section 4.2 <i>Java Package</i> of the specification. <p> <i><B>SPEC REQUIREMENT:</B> the provider must generate a class in each package that contains all of the necessary object factory methods for that  package named ObjectFactory as well as the static  <tt>newInstance( javaContentInterface )</tt> method</i>   </blockquote> <p> <b>Marshalling</b> <p> <blockquote> The <A HREF="../../../javax/xml/bind/Marshaller.html" title="interface in javax.xml.bind"><CODE>Marshaller</CODE></A> class provides the client application the ability to convert a Java content tree back into XML data.  There is no difference between marshalling a content tree that is created manually using the factory methods and marshalling a content tree that is the result an <tt>unmarshal </tt> operation.  Clients can marshal a java content tree back to XML data to a <tt>java.io.OutputStream</tt> or a <tt>java.io.Writer</tt>.  The  marshalling process can alternatively produce SAX2 event streams to a  registered <tt>ContentHandler</tt> or produce a DOM Node object.   Client applications have control over the output encoding as well as  whether or not to marshal the XML data as a complete document or  as a fragment. <p> Here is a simple example that unmarshals an XML document and then marshals it back out: <pre>        JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );        // unmarshal from foo.xml        Unmarshaller u = jc.createUnmarshaller();        FooObject fooObj = (FooObject)u.unmarshal( new File( "foo.xml" ) );        // marshal to System.out

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?