xmladapter.html

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

HTML
422
字号
<!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:59 PST 2008 --><TITLE>XmlAdapter (Java EE 5)</TITLE><META NAME="keywords" CONTENT="javax.xml.bind.annotation.adapters.XmlAdapter class"><LINK REL ="stylesheet" TYPE="text/css" HREF="../../../../../stylesheet.css" TITLE="Style"><SCRIPT type="text/javascript">function windowTitle(){    parent.document.title="XmlAdapter (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/annotation/adapters/NormalizedStringAdapter.html" title="class in javax.xml.bind.annotation.adapters"><B>PREV CLASS</B></A>&nbsp;&nbsp;<A HREF="../../../../../javax/xml/bind/annotation/adapters/XmlJavaTypeAdapter.html" title="annotation in javax.xml.bind.annotation.adapters"><B>NEXT CLASS</B></A></FONT></TD><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">  <A HREF="../../../../../index.html?javax/xml/bind/annotation/adapters/XmlAdapter.html" target="_top"><B>FRAMES</B></A>  &nbsp;&nbsp;<A HREF="XmlAdapter.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;FIELD&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;FIELD&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.annotation.adapters</FONT><BR>Class XmlAdapter&lt;ValueType,BoundType&gt;</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.annotation.adapters.XmlAdapter&lt;ValueType,BoundType&gt;</B></PRE><DL><DT><DT><B>Type Parameters:</B><DD><CODE>BoundType</CODE> - The type that JAXB doesn't know how to handle. An adapter is written      to allow this type to be used as an in-memory representation through      the <tt>ValueType</tt>.<DD><CODE>ValueType</CODE> - The type that JAXB knows how to handle out of the box.</DL><DL><DT><B>Direct Known Subclasses:</B> <DD><A HREF="../../../../../javax/xml/bind/annotation/adapters/CollapsedStringAdapter.html" title="class in javax.xml.bind.annotation.adapters">CollapsedStringAdapter</A>, <A HREF="../../../../../javax/xml/bind/annotation/adapters/HexBinaryAdapter.html" title="class in javax.xml.bind.annotation.adapters">HexBinaryAdapter</A>, <A HREF="../../../../../javax/xml/bind/annotation/adapters/NormalizedStringAdapter.html" title="class in javax.xml.bind.annotation.adapters">NormalizedStringAdapter</A></DD></DL><HR><DL><DT><PRE>public abstract class <B>XmlAdapter&lt;ValueType,BoundType&gt;</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>Adapts a Java type for custom marshaling. <p> <b> Usage: </b> </p> <p> Some Java types do not map naturally to a XML representation, for example <tt>HashMap</tt> or other non JavaBean classes. Conversely, a XML repsentation may map to a Java type but an application may choose to accesss the XML representation using another Java type. For example, the schema to Java binding rules bind xs:DateTime by default to XmlGregorianCalendar. But an application may desire to bind xs:DateTime to a custom type, MyXmlGregorianCalendar, for example. In both cases, there is a mismatch between <i> bound type </i>, used by an application to access XML content and the <i> value type</i>, that is mapped to an XML representation.   <p> This abstract class defines methods for adapting a bound type to a value type or vice versa. The methods are invoked by the JAXB binding framework during marshaling and unmarshalling: <ul>   <li> <b> XmlAdapter.marshal(...): </b> During marshalling, JAXB        binding framework invokes XmlAdapter.marshal(..) to adapt a        bound type to value type, which is then marshaled to XML         representation. </li>    <li> <b> XmlAdapter.unmarshal(...): </b> During unmarshalling,        JAXB binding framework first unmarshals XML representation        to a value type and then invokes XmlAdapter.unmarshal(..) to        adapt the value type to a bound type. </li>  </ul> Writing an adapter therefore involves the following steps:  <ul>   <li> Write an adapter that implements this abstract class. </li>   <li> Install the adapter using the annotation <A HREF="../../../../../javax/xml/bind/annotation/adapters/XmlJavaTypeAdapter.html" title="annotation in javax.xml.bind.annotation.adapters"><CODE>XmlJavaTypeAdapter</CODE></A> </li> </ul> <p><b>Example:</b> Customized mapping of </tt>HashMap</tt></p> <p> The following example illustrates the use of  <tt>&#64;XmlAdapter</tt> and <tt>&#64;XmlJavaTypeAdapter</tt> to customize the mapping of a <tt>HashMap</tt>. <p> <b> Step 1: </b> Determine the desired XML representation for HashMap. <pre>     &lt;hashmap>         &lt;entry key="id123">this is a value&lt;/entry>         &lt;entry key="id312">this is another value&lt;/entry>         ...       &lt;/hashmap>   </pre> <p> <b> Step 2: </b> Determine the schema definition that the desired XML representation shown above should follow. <pre>          &lt;xs:complexType name="myHashMapType">       &lt;xs:sequence>         &lt;xs:element name="entry" type="myHashMapEntryType"                        minOccurs = "0" maxOccurs="unbounded"/>       &lt;/xs:sequence>     &lt;/xs:complexType>     &lt;xs:complexType name="myHashMapEntryType">       &lt;xs:simpleContent>         &lt;xs:extension base="xs:string">           &lt;xs:attribute name="key" type="xs:int"/>         &lt;/xs:extension>       &lt;/xs:simpleContent>     &lt;/xs:complexType> </pre> <p> <b> Step 3: </b> Write value types that can generate the above schema definition. <pre>     public class MyHashMapType {         List&lt;MyHashMapEntryType> entry;     }     public class MyHashMapEntryType {         &#64;XmlAttribute         public Integer key;          &#64;XmlValue         public String value;     } </pre>  <p> <b> Step 4: </b> Write the adapter that adapts the value type, MyHashMapType to a bound type, HashMap, used by the application. <pre>     public final class MyHashMapAdapter extends                        XmlAdapter&lt;HashMap, MyHashMapType> { ... }       </pre> <p> <b> Step 5: </b> Use the adapter.

⌨️ 快捷键说明

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