⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cb4.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 4 页
字号:
&nbsp;&nbsp;response.getSOAPPart().getEnvelope().getBody();<a name="wp66443"> </a></pre></div><a name="wp66905"> </a><p class="pBody">The next step is to retrieve the <code class="cCode">SOAPBodyElement</code> object. The method <code class="cCode">getChildElements</code> returns an <code class="cCode">Iterator</code> object that contains all of the child elements of the element on which it is called, so in the following lines of code, <code class="cCode">it1</code> contains the <code class="cCode">SOAPBodyElement</code> object <code class="cCode">bodyEl</code>, which represents the <code class="cCode">price-list</code> element.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Iterator it1 = responseBody.getChildElements(); while (it1.hasNext()) {&nbsp;&nbsp;SOAPBodyElement bodyEl = (SOAPBodyElement)it1.next();<a name="wp66904"> </a></pre></div><a name="wp66958"> </a><p class="pBody">The <code class="cCode">Iterator</code> object <code class="cCode">it2</code> holds the child elements of <code class="cCode">bodyEl</code>, which represent <code class="cCode">coffee</code> elements. Calling the method <code class="cCode">next</code> on <code class="cCode">it2</code> retrieves the first coffee element in <code class="cCode">bodyEl</code>. As long as <code class="cCode">it2</code> has another element, the method <code class="cCode">next</code> will return the next <code class="cCode">coffee</code> element.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&nbsp;&nbsp;Iterator it2 = bodyEl.getChildElements();&nbsp;&nbsp;while (it2.hasNext()) {&nbsp;&nbsp;&nbsp;&nbsp;SOAPElement child2 = (SOAPElement)it2.next();<a name="wp66451"> </a></pre></div><a name="wp67044"> </a><p class="pBody">The next lines of code drill down another level to retrieve the <code class="cCode">coffee-name</code> and <code class="cCode">price</code> elements contained in <code class="cCode">it3</code>. Then the message <code class="cCode">getValue</code> retrieves the text (a coffee name or a price) that the SAAJ coffee distributor added to the <code class="cCode">coffee-name</code> and <code class="cCode">price</code> elements when it gave content to <code class="cCode">response</code>. The final line in the following code fragment adds the coffee name or price to the <code class="cCode">Vector</code> object <code class="cCode">list</code>. Note that because of the nested while loops, for each <code class="cCode">coffee</code> element that the code retrieves, both of its child elements (the <code class="cCode">coffee-name </code>and <code class="cCode">price</code> elements) are retrieved.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&nbsp;&nbsp;&nbsp;&nbsp;Iterator it3 = child2.getChildElements();&nbsp;&nbsp;&nbsp;&nbsp;while (it3.hasNext()) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SOAPElement child3 = (SOAPElement)it3.next();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String value = child3.getValue();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list.addElement(value);&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;}}<a name="wp67041"> </a></pre></div><a name="wp68917"> </a><p class="pBody">The last code fragment adds the coffee names and their prices (as a <code class="cCode">PriceListItem</code>) to the <code class="cCode">ArrayList</code> <code class="cCode">priceItems</code>, and prints each pair on a separate line. Finally it constructs and returns a <code class="cCode">PriceListBean</code>.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">ArrayList priceItems = new ArrayList();for (int i = 0; i &lt; list.size(); i = i + 2) {&nbsp;&nbsp;priceItems.add(&nbsp;&nbsp;&nbsp;&nbsp;new PriceItemBean(list.elementAt(i).toString(), &nbsp;&nbsp;&nbsp;&nbsp;new BigDecimal(list.elementAt(i + 1).toString())));&nbsp;&nbsp;System.out.print(list.elementAt(i) + &quot;        &quot;);&nbsp;&nbsp;System.out.println(list.elementAt(i + 1));}Date today = new Date();Date endDate = DateHelper.addDays(today, 30);Calendar todayCal = new GregorianCalendar();todayCal.setTime(today);Calendar cal = new GregorianCalendar();cal.setTime(endDate);plb = new PriceListBean();plb.setStartDate(todayCal);plb.setPriceItems(priceItems);plb.setEndDate(cal);<a name="wp72365"> </a></pre></div><a name="wp69071"> </a><h4 class="pHeading3">Ordering Coffee</h4><a name="wp67778"> </a><p class="pBody">The other kind of message that the Coffee Break server can send to the SAAJ distributor is an order for coffee. This is done in the <code class="cCode">placeOrder</code> method of <code class="cCode">OrderRequest</code>, which follows the DTD <code class="cCode">coffee-order.dtd</code>.</p><a name="wp67890"> </a><h5 class="pHeading4">Creating the Order</h5><a name="wp67893"> </a><p class="pBody">As with the client code for requesting a price list, the <code class="cCode">placeOrder</code> method starts out by creating a <code class="cCode">SOAPConnection</code> object, creating a <code class="cCode">SOAPMessage</code> object, and accessing the message's <code class="cCode">SOAPEnvelope</code> and <code class="cCode">SOAPBody</code> objects.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SOAPConnectionFactory scf =&nbsp;&nbsp;SOAPConnectionFactory.newInstance();SOAPConnection con = scf.createConnection();MessageFactory mf = MessageFactory.newInstance();SOAPMessage msg = mf.createMessage();SOAPPart part = msg.getSOAPPart();SOAPEnvelope envelope = part.getEnvelope();SOAPBody body = envelope.getBody();<a name="wp67834"> </a></pre></div><a name="wp67900"> </a><p class="pBody">Next the code creates and adds XML elements to form the order. As is required, the first element is a <code class="cCode">SOAPBodyElement</code>, which in this case is <code class="cCode">coffee-order</code>.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Name bodyName = envelope.createName(&quot;coffee-order&quot;, &quot;PO&quot;, &nbsp;&nbsp;&quot;http://sonata.coffeebreak.com&quot;); SOAPBodyElement order = body.addBodyElement(bodyName);<a name="wp67838"> </a></pre></div><a name="wp67914"> </a><p class="pBody">The application then adds the next level of elements, the first of these being <code class="cCode">orderID</code>. The value given to <code class="cCode">orderID</code> is extracted from the <code class="cCode">OrderBean</code> object passed to the <code class="cCode">OrderRequest</code>.<code class="cCode">placeOrder</code> method.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Name orderIDName = envelope.createName(&quot;orderID&quot;);SOAPElement orderID = order.addChildElement(orderIDName);orderID.addTextNode(orderBean.getId());<a name="wp67922"> </a></pre></div><a name="wp67930"> </a><p class="pBody">The next element, <code class="cCode">customer</code>, has several child elements that give information about the customer. This information is also extracted from the <code class="cCode">Customer</code> component of <code class="cCode">OrderBean</code>.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Name childName = envelope.createName(&quot;customer&quot;);SOAPElement customer = order.addChildElement(childName);childName = envelope.createName(&quot;last-name&quot;);SOAPElement lastName = customer.addChildElement(childName); &nbsp;&nbsp; lastName.addTextNode(orderBean.getCustomer().getLastName());childName = envelope.createName(&quot;first-name&quot;); SOAPElement firstName = customer.addChildElement(childName); &nbsp;&nbsp; firstName.addTextNode(orderBean.getCustomer().getFirstName());childName = envelope.createName(&quot;phone-number&quot;); SOAPElement phoneNumber = customer.addChildElement(childName); &nbsp;&nbsp;&nbsp;&nbsp; phoneNumber.addTextNode(&nbsp;&nbsp;orderBean.getCustomer().getPhoneNumber());childName = envelope.createName(&quot;email-address&quot;); SOAPElement emailAddress = &nbsp;&nbsp;customer.addChildElement(childName); emailAddress.addTextNode(&nbsp;&nbsp;orderBean.getCustomer().getEmailAddress());<a name="wp67840"> </a></pre></div><a name="wp67950"> </a><p class="pBody">The <code class="cCode">address</code> element, added next, has child elements for the street, city, state, and zip code. This information is extracted from the <code class="cCode">Address</code> component of <code class="cCode">OrderBean</code>.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">childName = envelope.createName(&quot;address&quot;);SOAPElement address = order.addChildElement(childName);childName = envelope.createName(&quot;street&quot;);SOAPElement street = address.addChildElement(childName);street.addTextNode(orderBean.getAddress().getStreet());childName = envelope.createName(&quot;city&quot;); &nbsp;&nbsp;SOAPElement city = address.addChildElement(childName); city.addTextNode(orderBean.getAddress().getCity());childName = envelope.createName(&quot;state&quot;);SOAPElement state = address.addChildElement(childName);state.addTextNode(orderBean.getAddress().getState());childName = envelope.createName(&quot;zip&quot;);SOAPElement zip = address.addChildElement(childName);zip.addTextNode(orderBean.getAddress().getZip());<a name="wp67953"> </a></pre></div><a name="wp67992"> </a><p class="pBody">The element <code class="cCode">line-item</code> has three child elements: <code class="cCode">coffeeName</code>, <code class="cCode">pounds</code>, and <code class="cCode">price</code>. This information is extracted from the <code class="cCode">LineItems</code> list contained in <code class="cCode">OrderBean</code>.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">for (Iterator it = orderBean.getLineItems().iterator();&nbsp;&nbsp;&nbsp;&nbsp;it.hasNext(); ) {&nbsp;&nbsp;LineItemBean lib = (LineItemBean)it.next();&nbsp;&nbsp;childName = envelope.createName(&quot;line-item&quot;);&nbsp;&nbsp;SOAPElement lineItem = order.addChildElement(childName);&nbsp;&nbsp;childName = envelope.createName(&quot;coffeeName&quot;);&nbsp;&nbsp;SOAPElement coffeeName = &nbsp;&nbsp;&nbsp;&nbsp;lineItem.addChildElement(childName);&nbsp;&nbsp;coffeeName.addTextNode(lib.getCoffeeName());&nbsp;&nbsp;childName = envelope.createName(&quot;pounds&quot;);&nbsp;&nbsp;SOAPElement pounds = lineItem.addChildElement(childName);&nbsp;&nbsp;pounds.addTextNode(lib.getPounds().toString());&nbsp;&nbsp;childName = envelope.createName(&quot;price&quot;);&nbsp;&nbsp;SOAPElement price = lineItem.addChildElement(childName);&nbsp;&nbsp;price.addTextNode(lib.getPrice().toString());}// totalchildName = envelope.createName(&quot;total&quot;);SOAPElement total = order.addChildElement(childName);total.addTextNode(orderBean.getTotal().toString()); <a name="wp72005"> </a></pre></div><a name="wp68078"> </a><p class="pBody">With the order complete, the application sends the message to the endpoint <code class="cCode">http://localhost:</code>8080<code class="cCode">/saaj-coffee-supplier/orderCoffee</code> and closes the connection.</p>

⌨️ 快捷键说明

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