解析WSDL的简单方法 [英] Simple way to parse a WSDL
问题描述
我正在尝试解析WSDL以获取操作,端点和示例有效负载.用户输入的WSDL.我找不到执行此操作的教程.
I am trying to parse a WSDL to get the operations, endpoint and an example payload. The WSDL in inputted by the user. I can't find a tutorial to do this.
我只能找到那些生成不需要的源代码的代码.我尝试使用XBeans,但显然我需要Saxon.没有撒克逊人,有没有简单的轻巧的方法来做到这一点?
I can only find ones that generate source code which I don't need. I've tried using XBeans but apparently I need Saxon. Is there a simple lightweight way to do this without 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
Should get operations: GetLastTradePrice, GetLastTradePrice
端点:StockQuotePort
Endpoint: 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应用程序中(文件上载必须转到servlet).因此,我需要解析文件并创建GWT能够理解的对象.
This is like what SoapUI does. But I'm mainly concerned with being able to parse the WSDL. A bit more context is the WSDL is uploaded and then the result is displayed in a GWT application (file upload must go to the servlet). So I need to parse the file and create an object the GWT will be able to understand.
推荐答案
这看起来不错:虽然对我而言,第一次尝试没有用,所以我写了一种方法,该方法返回示例wsdl的建议结果-没有J2SE6以外的依赖项.
Didn't work on first attempt for me though, So I wrote a method that returns the suggested results for the sample wsdl - no dependencies outside of 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中被列出两次.使用Set很容易.已上传的完整Eclipse项目,在此处显示了唯一和非唯一的结果: https://github.com/sek/wsdlparser
Seems like you would want to remove the duplicates, since each operation is listed twice in WSDL. That's easy using a Set. Uploaded complete eclipse project that shows both unique and non-unique results here: https://github.com/sek/wsdlparser
这篇关于解析WSDL的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!