📄 lib0073.html
字号:
<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>An XAO Example</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="LiB0072.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0074.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="343"></a><a name="ch11lev1sec1"></a>An XAO Example</h2><p class="first-para">XML access objects are responsible for translating XML documents into value objects that can be used by the rest of the application and vice versa. <a name="344"></a><a name="IDX-141"></a>For example, <a class="internaljump" href="#ch11list01">Listing 11.1</a> illustrates the setPurchaseOrder() method that reads an XML document and extracts an array of purchase order value objects. This example happens to use the JAXB API along with a CementJ utility to interpret XML. However, JDOM fans or developers who like the native DOM parser would place their extraction logic here. In fact, your XML parsing and interpretation strategy can change in XAOs without adversely affecting the rest of your application.</p>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Listing 11.1: </span>Sample XAO Method to Read an XML Document</span><a name="345"></a><a name="ch11list01"></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:package book.sample.dao.xml;
2:
3:import org.cementj.util.JAXBUtility;
4:// some imports omitted.
5:
6:public class PurchaseOrderXAO
7:{
8: private static final String
9: PURCHASE_ORDER_JAXB_PACKAGE =
10: "book.sample.dao.xml.po";
11:
12: public void setPurchaseOrder(InputStream xmlStream)
13: {
14: PurchaseOrderVO[] poArray = null;
15: ArrayList poList = new ArrayList();
16: PurchaseOrderVO po = null;
17: CustomerOrderType xmlOrder = null;
18:
19: try
20: {
21: CustomerOrderList order =
22: (CustomerOrderList)
23: JAXBUtility.getJaxbXmlObject(
24: PURCHASE_ORDER_JAXB_PACKAGE,
25: xmlStream);
26: List xmlOrderList = order.getCustomerOrder();
27: for (int i = 0 ; i < xmlOrderList.size(); i++)
28: {
29: xmlOrder = (CustomerOrderType)
30: xmlOrderList.get(i);
31: po = new PurchaseOrderVO();
32:
33: po.setCustomerId(xmlOrder.getCustomerId());
34: po.setOrderNbr(
35: Integer.parseInt(xmlOrder.getOrderId()));
36: // ... Other Purchase Order information
37: // gathered here.<a name="346"></a><a name="IDX-142"></a>
38:
39: poList.add(po);
40: }
41:
42: if (poList.size() > 0)
43: {
44: poArray = new PurchaseOrderVO[poList.size()];
45: poArray = (PurchaseOrderVO[])
46: poList.toArray(poArray);
47: }
48:
49: this.setPurchaseOrder(poArray);
50: }
51: catch (Throwable t)
52: {
53: throw new SampleException(
54: "Error parsing PO XML.", t);
55: }
56: }
57:}
</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/PurchaseOrderXAO.java</p>
<p class="para">I used a utility from CementJ in line 23 to save several lines of code. Line 23 could easily be replaced with code using JAXB or JDOM directly if you prefer.</p>
<p class="para">A method to create XML documents would be structured much the same way. The <span class="fixed">PurchaseOrderXAO</span> class could easily have a method called <span class="fixed">getPurchaseOrderXmlText()</span> that generates XML text, as illustrated in <a class="internaljump" href="#ch11list02">listing 11.2</a>.</p>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Listing 11.2: </span>Sample XAO Method to Create XML Text</span><a name="347"></a><a name="ch11list02"></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:package book.sample.dao.xml;
2:
3:import org.cementj.util.JAXBUtility;
4:// some imports omitted.
5:
6:public class PurchaseOrderXAO
7:{
8: private static final String
9: PURCHASE_ORDER_JAXB_PACKAGE =
10: "book.sample.dao.xml.po";
11:
12: public String getPurchaseOrderXmlText()
13: {
14: String xmlText = null;
15: ObjectFactory factory = new ObjectFactory();<a name="348"></a><a name="IDX-143"></a>
16:
17: CustomerOrderType xmlOrder = null;
18:
19: try
20: {
21: CustomerOrderList xmlOrderList =
22: factory.createCustomerOrderList();
23: for (int i = 0; i < _purchaseOrder.length; i++)
24: {
25: xmlOrder = factory.createCustomerOrderType();
26: xmlOrder.setCustomerId(
27: _purchaseOrder[i].getCustomerId());
28: xmlOrder.setOrderId(
29: Integer.toString(
30: _purchaseOrder[i].getOrderNbr()));
31: // ... Other Purchase Order information set
32: // here.
33:
34: xmlOrderList.getCustomerOrder().add(xmlOrder);
35: }
36:
37: xmlText = JAXBUtility.flushXmlToString(
38: PURCHASE_ORDER_JAXB_PACKAGE, xmlOrderList);
39: }
40: catch (JAXBException j)
41: {
42: throw new SampleException(
43: "Error creating PO XML.", j);
44: }
45:
46: return xmlText;
47: }
48:}
</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/PurchaseOrderXAO.java</p>
<p class="para">Notice that XML interpretation and generation are self-contained and localized. If attributes are added to the <span class="fixed"><purchase-order></span> XML document and are needed by the application, those changes are localized to XAO classes and those generated by JAXB. The XML document format can change without affecting the other layers of the application.</p>
<p class="para">Sometimes, XAOs are used to translate XML documents into alternative formats. A common technology to accomplish this is XSLT. As an example, I've created a short XSL template that translates the <span class="fixed"><purchase-order></span> XML document into HTML, which can be sent to a browser. The <span class="fixed">PurchaseOrderXAO</span> class could easily have a method called <a name="349"></a><a name="IDX-144"></a><span class="fixed">getPurchaseOrderAsHtml()</span> that generates XML text, as illustrated in listing11.3.</p>
<div class="example">
<span class="example-title"><span class="example-titlelabel">Listing 11.3: </span>Sample XAO Method to Create HTML Text</span><a name="350"></a><a name="ch11list03"></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:package book.sample.dao.xml;
2:
3:import javax.xml.transform.Transformer;
4:import javax.xml.transform.TransformerFactory;
5:import javax.xml.transform.stream.StreamResult;
6:import javax.xml.transform.stream.StreamSource;
7:// some imports omitted.
8:
9:public class PurchaseOrderXAO
10:{
11:
12: public String getPurchaseOrderAsHtml()
13: {
14: String htmlText = null;
15: String xmlText = this.getPurchaseOrderXmlText();
16: ByteArrayInputStream xmlTextStream =
17: new ByteArrayInputStream(xmlText.getBytes());
18:
19: try
20: {
21: ByteArrayOutputStream output =
22: new ByteArrayOutputStream
23: (xmlText.length() * 2);
24: TransformerFactory tFactory =
25: TransformerFactory.newInstance();
26: Transformer transformer =
27: tFactory.newTransformer
28: (new StreamSource("PurchaseOrder.xsl"));
29: transformer.transform(
30: new StreamSource(xmlTextStream),
31: new StreamResult(output));
32: htmlText = output.toString();
33: }
34: catch (Throwable t)
35: {
36: throw new SampleException(
37: "Error creating PO HTML.", t);
38: }
39:
40: return htmlText;
41: }
42:}
</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="last-para">
<i class="emphasis">Source</i>: /src/book/sample/dao/xml/PurchaseOrderXAO.java</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="LiB0072.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0074.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 + -