java简单解析wsdl文件

 1 package com.example.demo.api.soap.client.userInterface.controller;
 2 
 3 
 4 import org.w3c.dom.Document;
 5 import org.w3c.dom.NamedNodeMap;
 6 import org.w3c.dom.Node;
 7 import org.w3c.dom.NodeList;
 8 import org.xml.sax.SAXException;
 9 
10 import javax.xml.parsers.DocumentBuilder;
11 import javax.xml.parsers.DocumentBuilderFactory;
12 import javax.xml.parsers.ParserConfigurationException;
13 import java.io.File;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.net.URL;
17 
18 @SuppressWarnings("all")
19 public class UserInterfaceController {
20     public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
21         URL url = new URL("http://localhost:8081/userInfterface?wsdl");
22         InputStream openStream = url.openStream();
23         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
24         factory.setNamespaceAware(true);
25         DocumentBuilder builder = factory.newDocumentBuilder();
26         Document doc = builder.parse(openStream);
27 
28         System.out.println("root:" + doc.getFirstChild().getNodeName());
29 
30         NodeList nodeList = doc.getElementsByTagNameNS("http://schemas.xmlsoap.org/wsdl/", "definitions");
31         Node root = nodeList.item(0);
32         NamedNodeMap map = root.getAttributes();
33         Node targetNs = map.getNamedItem("targetNamespace");
34         System.out.println("名称空间:" + targetNs.getNodeValue());
35 
36         String wsdlNsUri = "http://schemas.xmlsoap.org/wsdl/";
37 
38         NodeList bindingNodes = doc.getElementsByTagNameNS(wsdlNsUri, "binding");
39         Node bindingNode = bindingNodes.item(0);
40         String serviceName = bindingNode.getAttributes().getNamedItem("name").getNodeValue();
41         System.out.println("服务名:" + serviceName);
42 
43         NodeList serviceNodes = doc.getElementsByTagNameNS(wsdlNsUri, "service");
44         Node serviceNode = serviceNodes.item(0);
45         NodeList portNodes = serviceNode.getChildNodes();
46 
47         String bindingAddress = null;
48         Node addressNode = portNodes.item(1).getChildNodes().item(1);
49         bindingAddress = addressNode.getAttributes().getNamedItem("location").getNodeValue();
50         System.out.println("调用地址:" + bindingAddress);
51 
52         NodeList portTypeNodes = doc.getElementsByTagNameNS(wsdlNsUri, "portType");
53         Node portType = portTypeNodes.item(0);
54         NodeList opNodes = portType.getChildNodes();
55         for (int i = 0; i < opNodes.getLength(); i++) {
56             Node node = opNodes.item(i);
57             if (node.getNodeType() == Node.ELEMENT_NODE && node.getLocalName().equalsIgnoreCase("operation")) {
58                 String methodName = node.getAttributes().getNamedItem("name").getNodeValue();
59                 System.out.println("方法名:" + methodName);
60             }
61         }
62     }
63 
64 }

 

posted @ 2022-11-26 00:36  勤快的懒羊羊  阅读(710)  评论(0编辑  收藏  举报