📄 cb4.html
字号:
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()) { 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"> Iterator it2 = bodyEl.getChildElements(); while (it2.hasNext()) { 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"> Iterator it3 = child2.getChildElements(); while (it3.hasNext()) { SOAPElement child3 = (SOAPElement)it3.next(); String value = child3.getValue(); list.addElement(value); } }}<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 < list.size(); i = i + 2) { priceItems.add( new PriceItemBean(list.elementAt(i).toString(), new BigDecimal(list.elementAt(i + 1).toString()))); System.out.print(list.elementAt(i) + " "); 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 = 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("coffee-order", "PO", "http://sonata.coffeebreak.com"); 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("orderID");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("customer");SOAPElement customer = order.addChildElement(childName);childName = envelope.createName("last-name");SOAPElement lastName = customer.addChildElement(childName); lastName.addTextNode(orderBean.getCustomer().getLastName());childName = envelope.createName("first-name"); SOAPElement firstName = customer.addChildElement(childName); firstName.addTextNode(orderBean.getCustomer().getFirstName());childName = envelope.createName("phone-number"); SOAPElement phoneNumber = customer.addChildElement(childName); phoneNumber.addTextNode( orderBean.getCustomer().getPhoneNumber());childName = envelope.createName("email-address"); SOAPElement emailAddress = customer.addChildElement(childName); emailAddress.addTextNode( 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("address");SOAPElement address = order.addChildElement(childName);childName = envelope.createName("street");SOAPElement street = address.addChildElement(childName);street.addTextNode(orderBean.getAddress().getStreet());childName = envelope.createName("city"); SOAPElement city = address.addChildElement(childName); city.addTextNode(orderBean.getAddress().getCity());childName = envelope.createName("state");SOAPElement state = address.addChildElement(childName);state.addTextNode(orderBean.getAddress().getState());childName = envelope.createName("zip");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(); it.hasNext(); ) { LineItemBean lib = (LineItemBean)it.next(); childName = envelope.createName("line-item"); SOAPElement lineItem = order.addChildElement(childName); childName = envelope.createName("coffeeName"); SOAPElement coffeeName = lineItem.addChildElement(childName); coffeeName.addTextNode(lib.getCoffeeName()); childName = envelope.createName("pounds"); SOAPElement pounds = lineItem.addChildElement(childName); pounds.addTextNode(lib.getPounds().toString()); childName = envelope.createName("price"); SOAPElement price = lineItem.addChildElement(childName); price.addTextNode(lib.getPrice().toString());}// totalchildName = envelope.createName("total");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 + -