解析WSDL的简单方法

Reb*_*zie 6 java parsing 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>
Run Code Online (Sandbox Code Playgroud)

应该获得操作: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>
Run Code Online (Sandbox Code Playgroud)

这就像SoapUI所做的那样.但我主要关心的是能够解析WSDL.更多上下文是上传WSDL,然后结果显示在GWT应用程序中(文件上载必须转到servlet).所以我需要解析文件并创建一个GWT能够理解的对象.

Sta*_*iel 8

这看起来不错: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()]);
}
Run Code Online (Sandbox Code Playgroud)

看起来你想要删除重复项,因为每个操作在WSDL中列出两次.使用Set很容易.上传完整的eclipse项目,在这里显示唯一和非唯一的结果:https://github.com/sek/wsdlparser