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> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A> </TD> <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../../../index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A> </TD> <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="../../../../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A> </TD> </TR></TABLE></TD><TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM></EM></TD></TR><TR><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2"> <A HREF="../../../../../javax/xml/bind/annotation/adapters/NormalizedStringAdapter.html" title="class in javax.xml.bind.annotation.adapters"><B>PREV CLASS</B></A> <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> <A HREF="XmlAdapter.html" target="_top"><B>NO FRAMES</B></A> <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: NESTED | FIELD | <A HREF="#constructor_summary">CONSTR</A> | <A HREF="#method_summary">METHOD</A></FONT></TD><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">DETAIL: FIELD | <A HREF="#constructor_detail">CONSTR</A> | <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<ValueType,BoundType></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<ValueType,BoundType></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<ValueType,BoundType></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>@XmlAdapter</tt> and <tt>@XmlJavaTypeAdapter</tt> to customize the mapping of a <tt>HashMap</tt>. <p> <b> Step 1: </b> Determine the desired XML representation for HashMap. <pre> <hashmap> <entry key="id123">this is a value</entry> <entry key="id312">this is another value</entry> ... </hashmap> </pre> <p> <b> Step 2: </b> Determine the schema definition that the desired XML representation shown above should follow. <pre> <xs:complexType name="myHashMapType"> <xs:sequence> <xs:element name="entry" type="myHashMapEntryType" minOccurs = "0" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> <xs:complexType name="myHashMapEntryType"> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="key" type="xs:int"/> </xs:extension> </xs:simpleContent> </xs:complexType> </pre> <p> <b> Step 3: </b> Write value types that can generate the above schema definition. <pre> public class MyHashMapType { List<MyHashMapEntryType> entry; } public class MyHashMapEntryType { @XmlAttribute public Integer key; @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<HashMap, MyHashMapType> { ... } </pre> <p> <b> Step 5: </b> Use the adapter.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?