java 简单解析wsdl

static void resolve(String wsdl) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();

    InputStream in = new ByteArrayInputStream(wsdl.getBytes("utf-8"));
    InputStreamReader isr = new InputStreamReader(in, "utf-8");

    InputSource is = new InputSource(isr);
    Document doc = builder.parse(is);

    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);
        }
    }
}

 

你可能感兴趣的:(java 简单解析wsdl)