📄 saaj3.html
字号:
SOAPMessage response = connection.call(message, endpoint);<a name="wp78670"> </a></pre></div><a name="wp78671"> </a><p class="pBody">The content of the message you sent is the stock symbol SUNW; the <code class="cCode">SOAPMessage</code> object <code class="cCode">response</code> should contain the last stock price for Sun Microsystems, which you will retrieve in the next section.</p><a name="wp78673"> </a><p class="pBody">A connection uses a fair amount of resources, so it is a good idea to close a connection as soon as you are through using it.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">connection.close();<a name="wp78675"> </a></pre></div><a name="wp78677"> </a><h4 class="pHeading3">Getting the Content of a Message</h4><a name="wp78678"> </a><p class="pBody">The initial steps for retrieving a message's content are the same as those for giving content to a message: Either you use the <code class="cCode">Message</code> object to get the <code class="cCode">SOAPBody</code> object, or you access the <code class="cCode">SOAPBody</code> object through the <code class="cCode">SOAPPart</code> and <code class="cCode">SOAPEnvelope</code> objects. </p><a name="wp78724"> </a><p class="pBody">Then you access the <code class="cCode">SOAPBody</code> object's <code class="cCode">SOAPBodyElement</code> object, because that is the element to which content was added in the example. (In a later section you will see how to add content directly to the <code class="cCode">SOAPPart</code> object, in which case you would not need to access the <code class="cCode">SOAPBodyElement</code> object for adding content or for retrieving it.) </p><a name="wp78727"> </a><p class="pBody">To get the content, which was added with the method <code class="cCode">SOAPElement.addTextNode</code>, you call the method <code class="cCode">Node.getValue</code>. Note that <code class="cCode">getValue</code> returns the value of the immediate child of the element that calls the method. Therefore, in the following code fragment, the method <code class="cCode">getValue</code> is called on <code class="cCode">bodyElement</code>, the element on which the method <code class="cCode">addTextNode</code> was called.</p><a name="wp78680"> </a><p class="pBody">In order to access <code class="cCode">bodyElement</code>, you need to call the method <code class="cCode">getChildElements</code> on <code class="cCode">soapBody</code>. Passing <code class="cCode">bodyName</code> to <code class="cCode">getChildElements</code> returns a <code class="cCode">java.util.Iterator</code> object that contains all of the child elements identified by the <code class="cCode">Name</code> object <code class="cCode">bodyName</code>. You already know that there is only one, so just calling the method <code class="cCode">next</code> on it will return the <code class="cCode">SOAPBodyElement</code> you want. Note that the method <code class="cCode">Iterator.next</code> returns a Java <code class="cCode">Object</code>, so it is necessary to cast the <code class="cCode">Object</code> it returns to a <code class="cCode">SOAPBodyElement</code> object before assigning it to the variable <code class="cCode">bodyElement</code>.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SOAPBody soapBody = response.getSOAPBody();java.util.Iterator iterator = soapBody.getChildElements(bodyName);SOAPBodyElement bodyElement = (SOAPBodyElement)iterator.next();String lastPrice = bodyElement.getValue();System.out.print(<code class="cCode">"</code>The last price for SUNW is <code class="cCode">");</code> System.out.println(lastPrice);<a name="wp78681"> </a></pre></div><a name="wp78682"> </a><p class="pBody">If there were more than one element with the name <code class="cCode">bodyName</code>, you would have had to use a <code class="cCode">while</code> loop using the method <code class="cCode">Iterator.hasNext</code> to make sure that you got all of them.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">while (iterator.hasNext()) { SOAPBodyElement bodyElement = (SOAPBodyElement)iterator.next(); String lastPrice = bodyElement.getValue(); System.out.print(<code class="cCode">"</code>The last price for SUNW is <code class="cCode">");</code> System.out.println(lastPrice);}<a name="wp78683"> </a></pre></div><a name="wp78684"> </a><p class="pBody">At this point, you have seen how to send a very basic request-response message and get the content from the response. The next sections provide more detail on adding content to messages.</p><a name="wp78691"> </a><h3 class="pHeading2">Adding Content to the Header</h3><a name="wp64105"> </a><p class="pBody">To add content to the header, you need to create a <code class="cCode">SOAPHeaderElement</code> object. As with all new elements, it must have an associated <code class="cCode">Name</code> object, which you can create using the message's <code class="cCode">SOAPEnvelope</code> object or a <code class="cCode">SOAPFactory</code> object.</p><a name="wp64106"> </a><p class="pBody">For example, suppose you want to add a conformance claim header to the message to state that your message conforms to the WS-I Basic Profile.</p><a name="wp78820"> </a><p class="pBody">The following code fragment retrieves the <code class="cCode">SOAPHeader</code> object from <code class="cCode">message</code> and adds a new <code class="cCode">SOAPHeaderElement</code> object to it. This <code class="cCode">SOAPHeaderElement</code> object contains the correct qualified name and attribute for a WS-I conformance claim header.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SOAPHeader header = message.getSOAPHeader();Name headerName = soapFactory.createName("Claim", "wsi", "http://ws-i.org/schemas/conformanceClaim/");SOAPHeaderElement headerElement = header.addHeaderElement(headerName);headerElement.addAttribute(soapFactory.createName( "conformsTo"), "http://ws-i.org/profiles/basic1.0/");<a name="wp78838"> </a></pre></div><a name="wp64108"> </a><p class="pBody">At this point, <code class="cCode">header</code> contains the <code class="cCode">SOAPHeaderElement</code> object <code class="cCode">headerElement</code> identified by the <code class="cCode">Name</code> object <code class="cCode">headerName</code>. Note that the <code class="cCode">addHeaderElement</code> method both creates <code class="cCode">headerElement</code> and adds it to <code class="cCode">header</code>. </p><a name="wp78854"> </a><p class="pBody">A conformance claim header has no content. This code produces the following XML header:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><SOAP-ENV:Header> <wsi:Claim conformsTo="http://ws-i.org/profiles/basic1.0/" xmlns:wsi="http://ws-i.org/schemas/conformanceClaim/"/></SOAP-ENV:Header><a name="wp78855"> </a></pre></div><a name="wp64109"> </a><p class="pBody">For more information about creating SOAP messages that conform to WS-I, see the Messaging section of the <code class="cCode"><a href="http://www.ws-i.org/Profiles/Basic/2003-01/BasicProfile-1.0-WGAD.html#messaging" target="_blank"></a></code>WS-I Basic Profile.</p><a name="wp78897"> </a><p class="pBody">For a different kind of header, you might want to add content to <code class="cCode">headerElement</code>. The following line of code uses the method <code class="cCode">addTextNode</code> to do this.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">headerElement.addTextNode("order");<a name="wp64110"> </a></pre></div><a name="wp64111"> </a><p class="pBody">Now you have the <code class="cCode">SOAPHeader</code> object <code class="cCode">header</code> that contains a <code class="cCode">SOAPHeaderElement</code> object whose content is <code class="cCode">"order"</code>.</p><a name="wp64112"> </a><h3 class="pHeading2">Adding Content to the SOAP Body</h3><a name="wp64113"> </a><p class="pBody">The process for adding content to the <code class="cCode">SOAPBody</code> object is the same as the process for adding content to the <code class="cCode">SOAPHeader</code> object. You access the <code class="cCode">SOAPBody</code> object, add a <code class="cCode">SOAPBodyElement</code> object to it, and add text to the <code class="cCode">SOAPBodyElement</code> object. It is possible to add additional <code class="cCode">SOAPBodyElement</code> objects, and it is possible to add subelements to the <code class="cCode">SOAPBodyElement</code> objects with the method <code class="cCode">addChildElement</code>. For each element or child element, you add content with the method <code class="cCode">addTextNode</code>.</p><a name="wp64114"> </a><p class="pBody">The following example shows adding multiple <code class="cCode">SOAPElement</code> objects and adding text to each of them. The code first creates the <code class="cCode">SOAPBodyElement</code> object <code class="cCode">purchaseLineItems</code>, which has a fully qualified name associated with it. That is, the <code class="cCode">Name</code> object for it has a local name, a namespace prefix, and a namespace URI. As you saw earlier, a <code class="cCode">SOAPBodyElement</code> object is required to have a fully qualified name, but child elements added to it, such as <code class="cCode">SOAPElement</code> objects, may have <code class="cCode">Name</code> objects with only the local name.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">SOAPBody body = soapFactory.getSOAPBody();Name bodyName = soapFactory.createName("PurchaseLineItems", "PO", "http://sonata.fruitsgalore.com");SOAPBodyElement purchaseLineItems = body.addBodyElement(bodyName);Name childName = soapFactory.createName("Order");SOAPElement order = purchaseLineItems.addChildElement(childName);childName = soapFactory.createName("Product");SOAPElement product = order.addChildElement(childName);product.addTextNode("Apple");childName = soapFactory.createName("Price");SOAPElement price = order.addChildElement(childName);price.addTextNode("1.56");childName = soapFactory.createName("Order");SOAPElement order2 = purchaseLineItems.addChildElement(childName);childName = soapFactory.createName("Product");SOAPElement product2 = order2.addChildElement(childName);product2.addTextNode("Peach");childName = soapFactory.createName("Price");SOAPElement price2 = order2.addChildElement(childName);price2.addTextNode("1.48");<a name="wp78927"> </a></pre></div><a name="wp64117"> </a><p class="pBody">The SAAJ code in the preceding example produces the following XML in the SOAP body:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><PO:PurchaseLineItems xmlns:PO="http://www.sonata.fruitsgalore/order"> <Order> <Product>Apple</Product> <Price>1.56</Price> </Order> <Order> <Product>Peach</Product> <Price>1.48</Price> </Order></PO:PurchaseLineItems><a name="wp64118"> </a></pre></div><a name="wp64119"> </a><h3 class="pHeading2">Adding Content to the SOAPPart Object</h3><a name="wp64120"> </a><p class="pBody">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -