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

📄 saaj3.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 5 页
字号:
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 = &nbsp;&nbsp;soapBody.getChildElements(bodyName);SOAPBodyElement bodyElement =&nbsp;&nbsp;(SOAPBodyElement)iterator.next();String lastPrice = bodyElement.getValue();System.out.print(<code class="cCode">&quot;</code>The last price for SUNW is <code class="cCode">&quot;);</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()) {&nbsp;&nbsp;SOAPBodyElement bodyElement = &nbsp;&nbsp;&nbsp;&nbsp;(SOAPBodyElement)iterator.next();&nbsp;&nbsp;String lastPrice = bodyElement.getValue();&nbsp;&nbsp;System.out.print(<code class="cCode">&quot;</code>The last price for SUNW is <code class="cCode">&quot;);</code>&nbsp;&nbsp;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(&quot;Claim&quot;,&nbsp;&nbsp;&quot;wsi&quot;, &quot;http://ws-i.org/schemas/conformanceClaim/&quot;);SOAPHeaderElement headerElement =&nbsp;&nbsp;header.addHeaderElement(headerName);headerElement.addAttribute(soapFactory.createName(&nbsp;&nbsp;&quot;conformsTo&quot;), &quot;http://ws-i.org/profiles/basic1.0/&quot;);<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">&lt;SOAP-ENV:Header&gt;  &lt;wsi:Claim conformsTo=&quot;http://ws-i.org/profiles/basic1.0/&quot;   xmlns:wsi=&quot;http://ws-i.org/schemas/conformanceClaim/&quot;/&gt;&lt;/SOAP-ENV:Header&gt;<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(&quot;order&quot;);<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">&quot;order&quot;</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(&quot;PurchaseLineItems&quot;,&nbsp;&nbsp;&quot;PO&quot;, &quot;http://sonata.fruitsgalore.com&quot;);SOAPBodyElement purchaseLineItems =&nbsp;&nbsp;body.addBodyElement(bodyName);Name childName = soapFactory.createName(&quot;Order&quot;);SOAPElement order =&nbsp;&nbsp;purchaseLineItems.addChildElement(childName);childName = soapFactory.createName(&quot;Product&quot;);SOAPElement product = order.addChildElement(childName);product.addTextNode(&quot;Apple&quot;);childName = soapFactory.createName(&quot;Price&quot;);SOAPElement price = order.addChildElement(childName);price.addTextNode(&quot;1.56&quot;);childName = soapFactory.createName(&quot;Order&quot;);SOAPElement order2 =&nbsp;&nbsp;purchaseLineItems.addChildElement(childName);childName = soapFactory.createName(&quot;Product&quot;);SOAPElement product2 = order2.addChildElement(childName);product2.addTextNode(&quot;Peach&quot;);childName = soapFactory.createName(&quot;Price&quot;);SOAPElement price2 = order2.addChildElement(childName);price2.addTextNode(&quot;1.48&quot;);<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">&lt;PO:PurchaseLineItems xmlns:PO=&quot;http://www.sonata.fruitsgalore/order&quot;&gt;  &lt;Order&gt;    &lt;Product&gt;Apple&lt;/Product&gt;    &lt;Price&gt;1.56&lt;/Price&gt;  &lt;/Order&gt;  &lt;Order&gt;    &lt;Product&gt;Peach&lt;/Product&gt;    &lt;Price&gt;1.48&lt;/Price&gt;  &lt;/Order&gt;&lt;/PO:PurchaseLineItems&gt;<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 + -