如何用Eclipse API 提供的 org.eclipse.wst.wsdl 去解析讀取WSDL檔案?

來源:互聯網
上載者:User

標籤:blog   http   io   ar   os   使用   sp   java   for   

相對來說,Eclipse API的中文資料比較少,但是Eclipse的API提供了非常多的,非常強大的功能。

比如,eclipse的Eclipse API 提供的 org.eclipse.wst.wsdl包,裡面提供了很多的類去解析WSDL檔案。

總體來說,其提供的API簡單易懂,而且其API是和專業術語對應起來的,比如,

一個WSDL文檔通常包含7個重要的元素,即types、import、message、portType、operation、binding、 service元素。
這些元素嵌套在definitions元素中,
(1) Definitions是WSDL文檔的根項目。對應於這個類: org.eclipse.wst.wsdl.Definition 其他的對象都可以通過這個對象獲得
(2) Types - 資料類型定義的容器,它使用某種類型系統(一般地使用XML Schema中的類型系統)。
(3) Message - 通訊訊息的資料結構的抽象類別型化定義。使用Types所定義的類型來定義整個訊息的資料結構。 
(4) PortType - 對於某個訪問進入點類型所支援的操作的抽象集合,這些操作可以由一個或多個服務存取點來支援。
 (子節點) Operation - 對服務中所支援的操作的抽象描述,一般單個Operation描述了一個訪問入口的請求/響應訊息對。
(5) Binding - 特定連接埠類型的具體協議和資料格式規範的綁定。
(6) Service- 相關服務存取點的集合。

(子節點) Port - 定義為協議/資料格式綁定與具體Web訪問地址組合的單個服務存取點。

下面是代碼執行個體:

import java.io.File;import java.io.IOException;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import javax.wsdl.Message;import javax.wsdl.Part;import javax.wsdl.PortType;import javax.xml.namespace.QName;import org.eclipse.emf.common.util.URI;import org.eclipse.emf.ecore.resource.Resource;import org.eclipse.emf.ecore.resource.ResourceSet;import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;import org.eclipse.wst.wsdl.Definition;import org.eclipse.wst.wsdl.Types;import org.eclipse.wst.wsdl.internal.impl.PartImpl;import org.eclipse.wst.wsdl.internal.util.WSDLResourceFactoryImpl;import org.eclipse.wst.wsdl.util.WSDLResourceImpl;import org.eclipse.xsd.XSDElementDeclaration;import org.eclipse.xsd.XSDSchema;import org.eclipse.xsd.util.XSDResourceImpl;import org.junit.Test;import org.junit.Before;public class WSDLParserWithEclipse {Definition definition=null;String wsdlPathString="E:/HelloEclipse-EMF-WSDL-XSD/test.wsdl";@Beforepublic void setup(){ResourceSet resourceSet = new ResourceSetImpl();Resource.Factory.Registry registry = resourceSet.getResourceFactoryRegistry();Map extensionToFactoryMap = registry.getExtensionToFactoryMap();extensionToFactoryMap.put("wsdl", new WSDLResourceFactoryImpl());File wsdlFile =new File(wsdlPathString);  URI uri = URI.createFileURI(wsdlFile.getAbsolutePath());// You can avoid this cast, but will have to cast anyway later to get the Definition out the resource contentsWSDLResourceImpl wsdlResource = (WSDLResourceImpl) resourceSet.createResource(uri);try {wsdlResource.load(null);definition = wsdlResource.getDefinition();}catch(Exception e){e.printStackTrace();}}    @Test    public void testTypes(){    Types types = definition.getETypes();    List schemas = types.getSchemas("http://www.xxxxx.com/problem");XSDSchema schema = (XSDSchema) schemas.get(0);    org.eclipse.xsd.util.XSDResourceImpl.serialize(System.out, schema.getElement());    }        @Test     public void testMessage(){    Map messages=definition.getMessages();    System.out.println("The message size is:"+messages.size());    Set setMessages=messages.keySet();Iterator iteratorMessages=setMessages.iterator();while(iteratorMessages.hasNext()){QName key=(QName)iteratorMessages.next();Message message=(Message)messages.get(key);//{http://www.xxxxx.com/problem}getKeysSoapIn//System.out.println("Message Name:"+message.getQName());if(message.getQName().toString().indexOf("getKeysSoapIn")>0){System.out.println("Message Name:"+message.getQName());Map partsMap=message.getParts();//org.eclipse.xsd.impl.XSDElementDeclarationImplSystem.out.println("Message Part size for getKeysSoapIn message is:"+partsMap.size());PartImpl part= (PartImpl)partsMap.get("problem");XSDElementDeclaration xsdElementDeclaration=part.getElementDeclaration();XSDResourceImpl.serialize(System.out, xsdElementDeclaration.getElement());}}    }    @Test     public void testPortType(){    Map portTypes=definition.getPortTypes();System.out.println("Port Type size:"+portTypes.size());if(portTypes!=null&&portTypes.size()>0){Set set=portTypes.keySet();Iterator iterator=set.iterator();while(iterator.hasNext()){QName object=(QName)iterator.next();PortType portType=(PortType)portTypes.get(object);System.out.println("Port Type name:"+portType.getQName());org.eclipse.xsd.util.XSDResourceImpl.serialize(System.out, portType.getDocumentationElement());}}    }}



如何用Eclipse API 提供的 org.eclipse.wst.wsdl 去解析讀取WSDL檔案?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.