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

📄 saaj4.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 4 页
字号:
</pre></div><a name="wp80674"> </a><p class="pBody">If you want to run <code class="cCode">MyUddiPing</code> again, you may want to start over by deleting the <code class="cCode">build</code> directory and the <code class="cCode">.class</code> file it contains. You can do this by typing the following at the command line:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">asant clean<a name="wp80675"> </a></pre></div><a name="wp71229"> </a><h3 class="pHeading2">HeaderExample.java</h3><a name="wp81436"> </a><p class="pBody">The example <code class="cCode">HeaderExample.java</code>, based on the code fragments in the section <a  href="SAAJ3.html#wp77353">Adding Attributes</a>, creates a message with several headers. It then retrieves the contents of the headers and prints them out. You will find the code for <code class="cCode">HeaderExample</code> in the following directory:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;<code class="cVariable">INSTALL</code>&gt;/j2eetutorial14/examples/saaj/headers/src/<a name="wp81444"> </a></pre></div><a name="wp81434"> </a><h4 class="pHeading3">Running HeaderExample</h4><a name="wp81470"> </a><p class="pBody">To run <code class="cCode">HeaderExample</code>, you use the file <code class="cCode">build.xml</code> that is in the directory &lt;<code class="cVariable">INSTALL</code>&gt;<code class="cCode">/j2eetutorial14/examples/saaj/headers/</code>.</p><a name="wp81471"> </a><p class="pBody">To run <code class="cCode">HeaderExample</code>, use the following command:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">asant run<a name="wp81472"> </a></pre></div><a name="wp81476"> </a><p class="pBody">This command executes the <code class="cCode">prepare</code>, <code class="cCode">build</code>, and <code class="cCode">run</code> targets in the <code class="cCode">build.xml </code>and <code class="cCode">targets.xml</code> files.</p><a name="wp81466"> </a><p class="pBody">When you run <code class="cCode">HeaderExample</code>, you will see output similar to the following:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">----- Request Message ----&lt;SOAP-ENV:Envelope xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;&lt;SOAP-ENV:Header&gt;&lt;ns:orderDesk SOAP-ENV:actor=&quot;http://gizmos.com/orders&quot; xmlns:ns=&quot;http://gizmos.com/NSURI&quot;/&gt;&lt;ns:shippingDesk SOAP-ENV:actor=&quot;http://gizmos.com/shipping&quot; xmlns:ns=&quot;http://gizmos.com/NSURI&quot;/&gt;&lt;ns:confirmationDesk SOAP-ENV:actor=&quot;http://gizmos.com/confirmations&quot; xmlns:ns=&quot;http://gizmos.com/NSURI&quot;/&gt;&lt;ns:billingDesk SOAP-ENV:actor=&quot;http://gizmos.com/billing&quot; xmlns:ns=&quot;http://gizmos.com/NSURI&quot;/&gt;&lt;t:Transaction SOAP-ENV:mustUnderstand=&quot;1&quot; xmlns:t=&quot;http://gizmos.com/orders&quot;&gt;5&lt;/t:Transaction&gt;&lt;/SOAP-ENV:Header&gt;&lt;SOAP-ENV:Body/&gt;&lt;/SOAP-ENV:Envelope&gt;Header name is ns:orderDeskActor is http://gizmos.com/ordersMustUnderstand is falseHeader name is ns:shippingDeskActor is http://gizmos.com/shippingMustUnderstand is falseHeader name is ns:confirmationDeskActor is http://gizmos.com/confirmationsMustUnderstand is falseHeader name is ns:billingDeskActor is http://gizmos.com/billingMustUnderstand is falseHeader name is t:TransactionActor is nullMustUnderstand is true<a name="wp81517"> </a></pre></div><a name="wp88886"> </a><h3 class="pHeading2">DOMExample.java and DomSrcExample.java</h3><a name="wp88889"> </a><p class="pBody">The examples <code class="cCode">DOMExample.java</code> and <code class="cCode">DOMSrcExample.java</code> show how to add a DOM document to a message and then to traverse its contents. They show two different ways to do this:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp88890"> </a><div class="pSmartList1"><li><code class="cCode">DomExample.java</code> creates a DOM document and adds it to the body of a message.</li></div><a name="wp88891"> </a><div class="pSmartList1"><li><code class="cCode">DomSrcExample.java</code> creates the document, uses it to create a <code class="cCode">DOMSource</code> object, and then sets the <code class="cCode">DOMSource</code> object as the content of the message's SOAP part.</li></div></ul></div><a name="wp81001"> </a><p class="pBody">You will find the code for <code class="cCode">DOMExample</code> and <code class="cCode">DOMSrcExample</code> in the following directory:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;<code class="cVariable">INSTALL</code>&gt;/j2eetutorial14/examples/saaj/dom/src/<a name="wp81009"> </a></pre></div><a name="wp88899"> </a><h4 class="pHeading3">Examining DOMExample</h4><a name="wp81039"> </a><p class="pBody"><code class="cCode">DOMExample</code> first creates a DOM document by parsing an XML document, almost exactly like the JAXP example <code class="cCode">DomEcho01.java</code> in the directory &lt;<code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/jaxp/dom/samples/</code>. The file it parses is one that you specify on the command line.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">static Document document;...&nbsp;&nbsp;DocumentBuilderFactory factory =&nbsp;&nbsp;&nbsp;&nbsp;DocumentBuilderFactory.newInstance();&nbsp;&nbsp;factory.setNamespaceAware(true);&nbsp;&nbsp;try {&nbsp;&nbsp;&nbsp;&nbsp;DocumentBuilder builder = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;factory.newDocumentBuilder();&nbsp;&nbsp;&nbsp;&nbsp;document = builder.parse( new File(args[0]) );&nbsp;&nbsp;&nbsp;&nbsp;...<a name="wp81081"> </a></pre></div><a name="wp81068"> </a><p class="pBody">Next, the example creates a SOAP message in the usual way. Then it adds the document to the message body:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&nbsp;&nbsp;&nbsp;&nbsp;SOAPBodyElement docElement = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;body.addDocument(document);<a name="wp81105"> </a></pre></div><a name="wp81117"> </a><p class="pBody">This example does not change the content of the message. Instead, it displays the message content and then uses a recursive method, <code class="cCode">getContents</code>, to traverse the element tree using SAAJ APIs and display the message contents in a readable form.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&nbsp;&nbsp;public void getContents(Iterator iterator, &nbsp;&nbsp;&nbsp;&nbsp;String indent) {&nbsp;&nbsp;&nbsp;&nbsp;while (iterator.hasNext()) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SOAPElement element = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(SOAPElement)iterator.next();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name name = element.getElementName();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(indent + &quot;Name is &quot; + &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name.getQualifiedName());&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String content = element.getValue();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (content != null) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(indent + &quot;Content is &quot; +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Iterator attrs = element.getAllAttributes();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (attrs.hasNext()){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name attrName = (Name)attrs.next();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(indent + &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot; Attribute name is &quot; + &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attrName.getQualifiedName());&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(indent + &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot; Attribute value is &quot; + &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;element.getAttributeValue(attrName));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (content == null) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Iterator iter2 = element.getChildElements();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getContents(iter2, indent + &quot; &quot;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;}<a name="wp81120"> </a></pre></div><a name="wp88908"> </a><h4 class="pHeading3">Examining DOMSrcExample</h4><a name="wp88909"> </a><p class="pBody"><code class="cCode">DOMSrcExample</code> differs from <code class="cCode">DomExample</code> in only a few ways. First, after it parses the document, it uses the document to create a <code class="cCode">DOMSource</code> object. This code is the same as that of <code class="cCode">DomExample</code> except for the last line:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&nbsp;&nbsp;static DOMSource domSource;&nbsp;&nbsp;...&nbsp;&nbsp;try {&nbsp;&nbsp;&nbsp;&nbsp;DocumentBuilder builder = &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;factory.newDocumentBuilder();&nbsp;&nbsp;&nbsp;&nbsp;document = builder.parse( new File(args[0]) );&nbsp;&nbsp;&nbsp;&nbsp;domSource = new DOMSource(document);&nbsp;&nbsp;&nbsp;&nbsp;...<a name="wp88910"> </a></pre></div><a name="wp88911"> </a><p class="pBody">Then, after <code class="cCode">DomSrcExample</code> creates the message, it does not get the header and body and add the document to the body, as <code class="cCode">DOMExample</code> does. Instead, it gets the SOAP part and sets the <code class="cCode">DOMSource</code> object as its content:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">// Create a messageSOAPMessage message = &nbsp;&nbsp;messageFactory.createMessage();// Get the SOAP part and set its content to domSourceSOAPPart soapPart = message.getSOAPPart();soapPart.setContent(domSource);<a name="wp88912"> </a></pre></div><a name="wp88916"> </a><p class="pBody">The example then uses the <code class="cCode">getContents</code> method to obtain the contents of both the header (if it exists) and the body of the message.</p><a name="wp88917"> </a><p class="pBody">The most important difference between these two examples is the kind of document you can use to create the message. Because <code class="cCode">DOMExample</code> adds the document to the body of the SOAP message, you can use any valid XML file to create the document. But because <code class="cCode">DOMSrcExample</code> makes the document the entire content of the message, the document must already be in the form of a valid SOAP message, not just any XML document.</p><a name="wp81011"> </a><h4 class="pHeading3">Running DOMExample and DOMSrcExample</h4><a name="wp81012"> </a><p class="pBody">To run <code class="cCode">DOMExample</code> and <code class="cCode">DOMSrcExample</code>, you use the file <code class="cCode">build.xml</code> that is in the directory &lt;<code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/saaj/dom/</code>. This directory also contains several sample XML files you can use:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp88929"> </a><div class="pSmartList1"><li><code class="cCode">domsrc1.xml</code>, an example that has a SOAP header (the contents of the <code class="cCode">HeaderExample</code> output) and the body of a UDDI query</li></div><a name="wp88930"> </a><div class="pSmartList1"><li><code class="cCode">domsrc2.xml</code>, an example of a reply to a UDDI query (specifically, some sample output from the <code class="cCode">MyUddiPing</code> example), but with spaces added for readability</li></div><a name="wp88931"> </a><div class="pSmartList1"><li><code class="cCode">uddimsg.xml</code>, similar to <code class="cCode">domsrc2.xml</code> except that it is only the body of the message and contains no spaces</li></div><a name="wp81311"> </a><div class="pSmartList1"><li><code class="cCode">slide.xml</code>, similar to the <code class="cCode">slideSample01.xml</code> file in &lt;<code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14</code><code class="cCode">/examples/jaxp/dom/samples/</code></li></div></ul></div><a name="wp81326"> </a><p class="pBody">To run <code class="cCode">DOMExample</code>, use a command like the following:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">asant run-dom -Dxml-file=uddimsg.xml<a name="wp81213"> </a></pre></div><a name="wp80999"> </a><p class="pBody">After running <code class="cCode">DOMExample</code>, you will see output something like the following:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Running DOMExample.

⌨️ 快捷键说明

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