📄 lib0075.html
字号:
<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Overview of JAXB</title>
<link rel="STYLESHEET" type="text/css" href="images/xpolecat.css">
<link rel="STYLESHEET" type="text/css" href="images/ie.content.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0074.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0076.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
<br>
<div class="chapter">
<a name="ch11"></a>
<div class="section">
<h2 class="first-section-title"><a name="353"></a><a name="ch11lev1sec3"></a>Overview of JAXB</h2><p class="first-para">Because JAXB is a relatively new API, here is a brief synopsis of JAXB and an example of how to use it in this section. JAXB can be downloaded from the Java Web site (<a target="_top" class="url" href="http://java.sun.com/xml/jaxb/">http://java.sun.com/xml/jaxb/</a>). Refer to the JAXB documentation for installation information.</p>
<p class="para">JAXB is a code generator that provides Java classes that mimic an XML document format. Each XML element is generated into a Java class with fields for all attributes on the XML document. For example, a <customerorder> XML element might be generated into a <span class="fixed">CustomerOrderType</span> interface that has accessors and mutators for all attributes and child elements of the segment. Programmers can then use the generated class as they would <a name="354"></a><a name="IDX-146"></a>any value object they would write. A diagram of the <span class="fixed">CustomerOrderType</span> interface generated by JAXB is presented in <a class="internaljump" href="#ch11fig02">Figure 11.2</a>.</p>
<div class="figure">
<a name="355"></a><a name="ch11fig02"></a><span class="figuremediaobject"><a href="images/fig162%5F01%5F0%2Ejpg" NAME="IMG_31" target="_parent"><img src="images/fig162_01.jpg" height="275" width="350" alt="Click To expand" border="0"></a></span>
<br style="line-height: 1">
<span class="figure-title"><span class="figure-titlelabel">Figure 11.2: </span>Object Model for JAXB-Generated Interface</span>
</div>
<p class="para">A similar interface and implementation was generated by JAXB for all elements in the schema. An important thing to note is that child elements are represented as lists. These generic lists contain only JAXB-generated components.</p>
<p class="para">Instantiating a JAXB object from a file containing an XML document is relatively easy. <a class="internaljump" href="#ch11list04">Listing 11.4</a> is an example.</p>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Listing 11.4: </span>Using JAXB to Read an XML Document</span><a name="356"></a><a name="ch11list04"></a>
<div class="formalbody">
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="Start example" border="0"></b></font></td>
</tr>
</table>
<pre class="literallayout">
1:import javax.xml.bind.JAXBContext;
2:import javax.xml.bind.Marshaller;
3:import javax.xml.bind.Unmarshaller;
4:
5:public class SampleJAXB
6:{<a name="357"></a><a name="IDX-147"></a>
7: public Object getJAXBObject() throws Throwable
8: {
9: InputStream xmlDocumentInputStream =
10: new FileInputStream("PurchaseOrder.xml");
11: JAXBContext jc = JAXBContext.newInstance(
12: "book.sample.dao.xml.po");
13: Unmarshaller u = jc.createUnmarshaller();
14: return u.unmarshal(xmlDocumentInputStream);
15: }
16:}
</pre>
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="End example" border="0"></b></font></td>
</tr>
</table>
<table class="BlankSpace" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td height="16"></td>
</tr>
</table>
</div>
</div>
<p class="para">
<i class="emphasis">Source:</i> /src/book/sample/dao/xml/SampleJAXB.java</p>
<p class="para">The JAXB calls are virtually identical for all JAXB documents. Alternatively, you can use the CementJ <span class="fixed">JAXBUtility</span> (from package <span class="fixed">org.cementj.util</span>) as a shortcut, as shown in <a class="internaljump" href="#ch11list05">listing 11.5</a>.</p>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Listing 11.5: </span>Using CementJ JAXBUtility to Read an XML Document</span><a name="358"></a><a name="ch11list05"></a>
<div class="formalbody">
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="Start example" border="0"></b></font></td>
</tr>
</table>
<pre class="literallayout">
1:import org.cementj.util.JAXBUtility;
2:
3:public class SampleJAXB
4:{
5: public Object getJAXBObjectViaCementJ()
6: throws Throwable
7: {
8: return JAXBUtility.getJaxbXmlObject(
9: "book.sample.dao.xml.po",
10: new File("PurchaseOrder.xml"));
11: }
12:}
</pre>
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="End example" border="0"></b></font></td>
</tr>
</table>
<table class="BlankSpace" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td height="16"></td>
</tr>
</table>
</div>
</div>
<p class="para">
<i class="emphasis">Source:</i> /src/book/sample/dao/xml/SampleJAXB.java</p>
<p class="para">Similarly, producing an XML document from a JAXB-binded object is also relatively easy. <a class="internaljump" href="#ch11list06">Listing 11.6</a> provides an example.</p>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Listing 11.6: </span>Using JAXB to Write an XML Document to Disk</span><a name="359"></a><a name="ch11list06"></a>
<div class="formalbody">
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="Start example" border="0"></b></font></td>
</tr>
</table>
<pre class="literallayout">
1:import javax.xml.bind.JAXBContext;
2:import javax.xml.bind.Marshaller;
3:import javax.xml.bind.Unmarshaller;
4:
5:public class SampleJAXB
6:{
7: public void serializeJAXBObject(
8: CustomerOrderList order)
9: throws Throwable
10: {<a name="360"></a><a name="IDX-148"></a>
11: JAXBContext jc = JAXBContext.newInstance(
12: "book.sample.dao.xml.po");
13: Marshaller m = jc.createMarshaller();
14: m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
15: Boolean.TRUE);
16: m.marshal(order, new FileOutputStream(
17: "PurchaseOrderGenerated.xml"));
18: }
19:}
</pre>
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="End example" border="0"></b></font></td>
</tr>
</table>
<table class="BlankSpace" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td height="16"></td>
</tr>
</table>
</div>
</div>
<p class="para">
<i class="emphasis">Source:</i> /src/book/sample/dao/xml/SampleJAXB.java</p>
<p class="para">CementJ also provides a shortcut for writing XML documents, as <a class="internaljump" href="#ch11list07">listing 11.7</a> illustrates.</p>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Listing 11.7: </span>Using CementJ to Produce an XML Document with JAXB Classes</span><a name="361"></a><a name="ch11list07"></a>
<div class="formalbody">
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="Start example" border="0"></b></font></td>
</tr>
</table>
<pre class="literallayout">
1:import org.cementj.util.JAXBUtility;
2:
3:public class SampleJAXB
4:{
5: public void serializeJAXBObjectWithCementJ(
6: CustomerOrderList order)
7: throws Throwable
8: {
9: JAXBUtility.flushXmlToStream(
10: "book.sample.dao.xml.po",
11: order, new FileOutputStream(
12: "PurchaseOrderGeneratedCementJ.xml"));
13: }
14:}
</pre>
<table class="BlueLine" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td bgcolor="000080" class="bluecell"><font size="2" face="Arial" color="010100"><b><img src="_.gif" width="1" height="2" alt="End example" border="0"></b></font></td>
</tr>
</table>
<table class="BlankSpace" border="0" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td height="16"></td>
</tr>
</table>
</div>
</div>
<p class="para">
<i class="emphasis">Source:</i> /src/book/sample/dao/xml/SampleJAXB.java</p>
<p class="para">JAXB requires an XML schema as input to the code generator. If you haven't developed schemas for XML documents, you can use one of a number of tools available that derive schemas from XML documents. These will relieve you of the job of coding the schemas manually. I use a commercial tool called xmlspy (<a target="_top" class="url" href="http://www.xmlspy.com/">http://www.xmlspy.com/</a>) to generate schemas from XML documents. You can find several other commercial and open source alternatives by doing a simple Web search.</p>
<p class="para">You can run the JAXB code generator from a command prompt or an ANT script. Because the documentation contains several good examples of invoking the JAXB code generator from an ANT script, I won't duplicate that effort here. To invoke JAXB from a Windows command prompt, use the following:</p>
<a name="362"></a><a name="IDX-149"></a>
<div class="informalexample">
<pre class="literallayout">
java -jar %JAXB_HOME%/lib/jaxb-xjc.jar -d <output dir> -p <package name>
<schema>
</pre>
</div>
<p class="last-para">For more information, refer to the JAXB documentation.</p>
</div>
</div><br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0074.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0076.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -