2012-05-16 3 views
5

작업, 끝점 및 예제 페이로드를 얻기 위해 WSDL을 구문 분석하려고합니다. 사용자가 입력 한 WSDL입니다. 이 작업을 수행하는 데 필요한 자습서를 찾을 수 없습니다.WSDL을 파싱하는 간단한 방법

내가 필요로하지 않는 소스 코드를 생성하는 소스 만 찾을 수 있습니다. XBeans를 사용해 보았지만 분명히 Saxon이 필요합니다. Saxon 없이는 이것을 할 수있는 간단한 방법이 있습니까?

예.

<?xml version="1.0"?> 
    <definitions name="StockQuote" 
    targetNamespace= 
    "http://example.com/stockquote.wsdl" 
    xmlns:tns="http://example.com/stockquote.wsdl" 
    xmlns:xsd1="http://example.com/stockquote.xsd" 
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
    xmlns="http://schemas.xmlsoap.org/wsdl/"> 
    <types> 
    <schema targetNamespace= 
    "http://example.com/stockquote.xsd" 
    xmlns="http://www.w3.org/2000/10/XMLSchema"> 
     <element name="TradePriceRequest"> 
     <complexType> 
      <all> 
      <element name="tickerSymbol" 
       type="string"/> 
      </all> 
     </complexType> 
     </element> 
     <element name="TradePrice"> 
     <complexType> 
      <all> 
      <element name="price" type="float"/> 
      </all> 
     </complexType> 
     </element> 
    </schema> 
    </types> 
    <message name="GetLastTradePriceInput"> 
    <part name="body" element= 
     "xsd1:TradePriceRequest"/> 
    </message> 
    <message name="GetLastTradePriceOutput"> 
    <part name="body" element="xsd1:TradePrice"/> 
    </message> 
    <portType name="StockQuotePortType"> 
    <operation name="GetLastTradePrice"> 
     <input message="tns:GetLastTradePriceInput"/> 
     <output message="tns:GetLastTradePriceOutput"/> 
    </operation> 
    </portType> 
    <binding name="StockQuoteSoapBinding" 
     type="tns:StockQuotePortType"> 
     <soap:binding style="document" 
     transport= 
      "http://schemas.xmlsoap.org/soap/http"/> 
    <operation name="GetLastTradePrice"> 
     <soap:operation 
     soapAction= 
      "http://example.com/GetLastTradePrice"/> 
     <input> 
      <soap:body use="literal"/> 
     </input> 
     <output> 
      <soap:body use="literal"/> 
     </output> 
     </operation> 
    </binding> 
    <service name="StockQuoteService"> 
     <documentation>My first service</documentation> 
     <port name="StockQuotePort" 
     binding="tns:StockQuoteBinding"> 
     <soap:address location= 
      "http://example.com/stockquote"/> 
     </port> 
    </service> 
    </definitions> 

는 작업을 얻을해야 : GetLastTradePrice,

엔드 포인트 GetLastTradePrice

: StockQuotePort

샘플 페이로드 :

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:stoc="http://example.com/stockquote.xsd"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <stoc:TradePriceRequest/> 
    </soapenv:Body> 
</soapenv:Envelope> 

이 SoapUI가하는 일 같다. 하지만 주로 WSDL을 파싱 할 수 있는지에 관심이 있습니다. WSDL이 업로드되고 GWT 응용 프로그램에 결과가 표시됩니다 (파일 업로드는 서블릿으로 이동해야합니다). 그래서 파일을 파싱하고 GWT가 이해할 수있는 객체를 만들어야합니다.

+0

예를 들어 wsdl 있으십니까? –

+2

wsdl은 XML 파서를 사용하여 구문 분석하여 필요한 것을 얻을 수 있습니다. SAX는 매우 가볍고 배우기 쉽습니다. http://stackoverflow.com/questions/2134507/fast-lightweight-xml-parser – Pedantic

+0

트릭을 수행 할 수있는 라이브러리를 찾는 것처럼 보입니다. SOAPUI에는 재사용 할 수있는 라이브러리가 있습니다. jar/class 이름은 기억이 나지 않지만 1 년 전 successfuly했습니다. – Abhilash

답변

7

이 좋은 같습니다 http://www.membrane-soa.org/soa-model-doc/1.4/java-api/parse-wsdl-java-api.htm

그래도 나를 위해 첫 번째 시도에서 작동하지 않았다, 그래서 샘플 WSDL에 대한 제안 된 결과를 반환하는 방법을 썼다 - J2SE6 외부 종속성.

public String[] listOperations(String filename) throws FileNotFoundException, SAXException, IOException, ParserConfigurationException { 
    Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new FileInputStream(filename)); 
    NodeList elements = d.getElementsByTagName("operation"); 
    ArrayList<String> operations = new ArrayList<String>(); 
    for (int i = 0; i < elements.getLength(); i++) { 
    operations.add(elements.item(i).getAttributes().getNamedItem("name").getNodeValue()); 
    } 
    return operations.toArray(new String[operations.size()]); 
} 

각 작업이 WSDL에 두 번 나열되어 있기 때문에 당신이 중복을 제거 할 것처럼 보인다. 세트를 사용하면 간단합니다. 독특하고 독특한 결과를 모두 보여주는 완벽한 이클립스 프로젝트를 업로드했습니다 : https://github.com/sek/wsdlparser

0

안녕하세요 @Rebzie는 매우 쉽고 가볍습니다. 나는 XML 파일을 파싱하는 것을 사용한다. 나는 너를 도울 수 있기를 바란다. :)

0
private static String getMethodNameFromWSDL(String wsdlPath) throws FileNotFoundException, SAXException, IOException, ParserConfigurationException { 

     String tagPrefix = null; 
     String namespace =null; 
     String location = null; 
     NodeList nd = null; 
     Set<String> operations = new HashSet<String>(); 
     NodeList nodeListOfOperations = null; 
     String attr ="http://www.w3.org/2001/XMLSchema" 

     Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(convertURLToString(wsdlPath).getBytes())); 

     NodeList allNodesOfDocumnet = document.getChildNodes(); 

     for(int index = 0;index<allNodesOfDocumnet.getLength();index++){ 
      if(document.getFirstChild().getNodeName().equalsIgnoreCase("#comment")){ 
        document.removeChild(document.getFirstChild()); 
      }  
     } 

     int l = document.getFirstChild().getAttributes().getLength(); 
     for (int i = 0; i < l; i++) { 
     String cmpAttribute = document.getFirstChild().getAttributes().item(i).getNodeValue(); 
      if(cmpAttribute.equals(attr)){ 
       tagPrefix = document.getFirstChild().getAttributes().item(i).getNodeName().replace("xmlns:", ""); 
      } 
     } 

     System.out.println(tagPrefix); 
     String str1=tagPrefix+":import"; 
     String str2="wsdl:import"; 
     String str3 ="operation"; 
     String str4 ="wsdl:operation"; 
     // Getting Namespace 
       if((document.getElementsByTagName(str1).getLength()>0)||(document.getElementsByTagName(str2).getLength()>0)){ 

          if(document.getElementsByTagName(tagPrefix+":import").getLength()>0) 
           nd =document.getElementsByTagName(tagPrefix+":import"); 

          else if (document.getElementsByTagName("wsdl:import").getLength()>0) 
           nd =document.getElementsByTagName("wsdl:import"); 

          for (int k = 0; k < nd.item(0).getAttributes().getLength(); k++) { 
           String strAttributes = nd.item(0).getAttributes().item(k).getNodeName(); 

           if(strAttributes.equalsIgnoreCase("namespace")){ 
            namespace = nd.item(0).getAttributes().item(k).getNodeValue(); 
            System.out.println("Namespace : "+namespace); 
           } 
           else { 
            location = nd.item(0).getAttributes().item(k).getNodeValue(); 
            System.out.println("Location : "+location); 
           } 

          } 
       }else{ 
         namespace = document.getFirstChild().getAttributes().getNamedItem("targetNamespace").getNodeValue(); 
         System.out.println("Namespace : "+namespace); 
        } 



       //Getting Operations 

       if((document.getElementsByTagName(str3).getLength()>0)||(document.getElementsByTagName(str4).getLength()>0)){ 

        if(document.getElementsByTagName(str3).getLength()>0){ 
         nodeListOfOperations =document.getElementsByTagName(str3); 
        } 
        else if (document.getElementsByTagName(str4).getLength()>0) { 
         nodeListOfOperations =document.getElementsByTagName(str4); 
        } 
        for (int i = 0; i < nodeListOfOperations.getLength(); i++) { 
          operations.add(nodeListOfOperations.item(i).getAttributes().getNamedItem("name").getNodeValue()); 
        } 

       } 

      if(location!=null){ 
       if(operations.isEmpty()){ 
        Document documentForOperation = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(convertURLToString(location).getBytes())); 
        NodeList nodesOfNewDoc = documentForOperation.getChildNodes(); 
        for(int index = 0;index<nodesOfNewDoc.getLength();index++){ 
         if(documentForOperation.getFirstChild().getNodeName().equalsIgnoreCase("#comment")){ 
           document.removeChild(document.getFirstChild()); 
         }  
        } 

        NodeList nodeList = documentForOperation.getElementsByTagName(str4); 
        for (int i = 0; i < nodeList.getLength(); i++) { 
         operations.add(nodeList.item(i).getAttributes().getNamedItem("name").getNodeValue()); 
        } 
       } 

      } 

      for (String string : operations) { 
       System.out.println(string); 
      } 
     System.out.println(operations.toString());   
     return operations.toString();   
    } 

public static String convertURLToString(String url){ 

    StringBuilder response = null; 

    try {  

     URL urlObj = new URL(url); 
     URLConnection urlConnection = urlObj.openConnection(); 
     urlConnection.setDoOutput(true); 
     urlConnection.connect();  
     //urlConnection.setConnectTimeout(30000);  
     BufferedReader reader = new BufferedReader(new 
      InputStreamReader(urlConnection.getInputStream())); 
     response = new StringBuilder(); 
     String inputLine; 

     while ((inputLine = reader.readLine()) != null){ 
      response.append(inputLine); 
     } 

     reader.close(); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return response.toString();  
} 
+0

나는 그것의 긴 것을 알고있다. 그러나 잘하면 그것은 잘될 것이다. –

관련 문제