2012-05-16 50 views
5

Tôi đang cố gắng phân tích cú pháp WSDL để nhận các thao tác, điểm cuối và tải trọng ví dụ. WSDL được người dùng nhập vào. Tôi không thể tìm thấy một hướng dẫn để làm điều này.Cách đơn giản để phân tích cú pháp WSDL

Tôi chỉ có thể tìm thấy những người tạo mã nguồn mà tôi không cần. Tôi đã thử sử dụng XBeans nhưng dường như tôi cần Saxon. Có một cách nhẹ đơn giản để làm điều này mà không có Saxon?

Ví dụ:

<?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> 

có nên hoạt động: GetLastTradePrice, GetLastTradePrice

Endpoint: StockQuotePort

mẫu Payload:

<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> 

này cũng giống như những gì soapUI làm. Nhưng tôi chủ yếu quan tâm đến việc có thể phân tích WSDL. Ngữ cảnh nhiều hơn một chút là WSDL được tải lên và sau đó kết quả được hiển thị trong một ứng dụng GWT (tệp tải lên phải đi tới servlet). Vì vậy, tôi cần phải phân tích cú pháp tệp và tạo một đối tượng mà GWT sẽ có thể hiểu được.

+0

Bạn có ví dụ về wsdl? –

+2

Wsdl có thể được phân tích bằng cách sử dụng một trình phân tích cú pháp XML để có được những gì bạn cần. SAX rất nhẹ và rất dễ học. Xem http://stackoverflow.com/questions/2134507/fast-lightweight-xml-parser – Pedantic

+0

Có vẻ như bạn đang tìm kiếm thư viện có thể thực hiện thủ thuật. SOAPUI có một số thư viện mà bạn có thể sử dụng lại. Tôi không nhớ tên jar/class nhưng tôi đã làm nó thành công 1 năm trước. – Abhilash

Trả lời

7

này có vẻ tốt đẹp: http://www.membrane-soa.org/soa-model-doc/1.4/java-api/parse-wsdl-java-api.htm

đã không hoạt động trên nỗ lực đầu tiên đối với tôi mặc dù, Vì vậy, tôi đã viết một phương thức trả về các kết quả gợi ý cho wsdl mẫu - không phụ thuộc bên ngoài của 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()]); 
} 

Có vẻ như bạn muốn xóa các bản sao, vì mỗi thao tác được liệt kê hai lần trong WSDL. Đó là dễ dàng bằng cách sử dụng một Set. Dự án nhật thực hoàn chỉnh đã hiển thị cả kết quả độc đáo và không độc đáo ở đây: https://github.com/sek/wsdlparser

0

Xin chào @Rebzie bạn có thể sử dụng JDOM rất dễ dàng và nhẹ không. Tôi sử dụng phân tích cú pháp tệp XML. Tôi hy vọng giúp bạn. :)

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

Tôi biết nó dài nhưng hy vọng nó sẽ hoạt động –

Các vấn đề liên quan