java简单解析wsdl文件

java code:

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;

public class wenservice {
    public static void main(String[] args) throws Exception {
        File file = new File("test/src/test.wsdl");
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(file);

        System.out.println("root:" + doc.getFirstChild().getNodeName());

        NodeList nodeList = doc.getElementsByTagNameNS("http://schemas.xmlsoap.org/wsdl/", "definitions");
        Node root = nodeList.item(0);
        NamedNodeMap map = root.getAttributes();
        Node targetNs = map.getNamedItem("targetNamespace");
        System.out.println("名称空间:" + targetNs.getNodeValue());

        String wsdlNsUri = "http://schemas.xmlsoap.org/wsdl/";

        NodeList bindingNodes = doc.getElementsByTagNameNS(wsdlNsUri, "binding");
        Node bindingNode = bindingNodes.item(0);
        String serviceName = bindingNode.getAttributes().getNamedItem("name").getNodeValue();
        System.out.println("服务名:" + serviceName);

        NodeList serviceNodes = doc.getElementsByTagNameNS(wsdlNsUri, "service");
        Node serviceNode = serviceNodes.item(0);
        NodeList portNodes = serviceNode.getChildNodes();

        String bindingAddress = null;
        Node addressNode = portNodes.item(1).getChildNodes().item(1);
        bindingAddress = addressNode.getAttributes().getNamedItem("location").getNodeValue();
        System.out.println("调用地址:" + bindingAddress);

        NodeList portTypeNodes = doc.getElementsByTagNameNS(wsdlNsUri, "portType");
        Node portType = portTypeNodes.item(0);
        NodeList opNodes = portType.getChildNodes();
        for (int i = 0; i < opNodes.getLength(); i++) {
            Node node = opNodes.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE && node.getLocalName().equalsIgnoreCase("operation")) {
                String methodName = node.getAttributes().getNamedItem("name").getNodeValue();
                System.out.println("方法名:" + methodName);
            }
        }
    }
}

wsdl 文件:

<?xml version="1.0" encoding="UTF-8"?>

<wsdl:definitions xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://test.demo1/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="HelloWorldService" targetNamespace="http://test.demo1/">

    <wsdl:types>

        <xs:schema xmlns:tns="http://test.demo1/" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" targetNamespace="http://test.demo1/" version="1.0">
            <xs:element name="sayHello" type="tns:sayHello"/>

            <xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>

            <xs:complexType name="sayHello">

                <xs:sequence>

                      <xs:element minOccurs="0" name="arg0" type="xs:string"/>

                </xs:sequence>

            </xs:complexType>

            <xs:complexType name="sayHelloResponse">

                <xs:sequence>

                      <xs:element minOccurs="0" name="return" type="xs:string"/>

                </xs:sequence>

            </xs:complexType>

        </xs:schema>

    </wsdl:types>
    <wsdl:message name="sayHelloResponse">

        <wsdl:part element="tns:sayHelloResponse" name="parameters">

        </wsdl:part>

    </wsdl:message>

    <wsdl:message name="sayHello">

        <wsdl:part element="tns:sayHello" name="parameters">

        </wsdl:part>

    </wsdl:message>
    <wsdl:portType name="HelloWorld">

        <wsdl:operation name="sayHello">

            <wsdl:input message="tns:sayHello" name="sayHello">

            </wsdl:input>

            <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">

            </wsdl:output>

        </wsdl:operation>

    </wsdl:portType>
    <wsdl:binding name="HelloWorldServiceSoapBinding" type="tns:HelloWorld">

        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

        <wsdl:operation name="sayHello">

            <soap:operation soapAction="" style="document"/>

            <wsdl:input name="sayHello">

                  <soap:body use="literal"/>

            </wsdl:input>

            <wsdl:output name="sayHelloResponse">

                  <soap:body use="literal"/>

            </wsdl:output>

        </wsdl:operation>

    </wsdl:binding>

    <wsdl:service name="HelloWorldService">

          <wsdl:port binding="tns:HelloWorldServiceSoapBinding" name="HelloWorldPort">

            <soap:address location="http://localhost:8080/helloWorld"/>

          </wsdl:port>

    </wsdl:service>

</wsdl:definitions>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值