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 16 @SuppressWarnings("all") 17 public class UserInterfaceController { 18 public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException { 19 File file = new File("c:\\user.wsdl"); 20 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 21 factory.setNamespaceAware(true); 22 DocumentBuilder builder = factory.newDocumentBuilder(); 23 Document doc = builder.parse(file); 24 25 System.out.println("root:" + doc.getFirstChild().getNodeName()); 26 27 NodeList nodeList = doc.getElementsByTagNameNS("http://schemas.xmlsoap.org/wsdl/", "definitions"); 28 Node root = nodeList.item(0); 29 NamedNodeMap map = root.getAttributes(); 30 Node targetNs = map.getNamedItem("targetNamespace"); 31 System.out.println("名称空间:" + targetNs.getNodeValue()); 32 33 String wsdlNsUri = "http://schemas.xmlsoap.org/wsdl/"; 34 35 NodeList bindingNodes = doc.getElementsByTagNameNS(wsdlNsUri, "binding"); 36 Node bindingNode = bindingNodes.item(0); 37 String serviceName = bindingNode.getAttributes().getNamedItem("name").getNodeValue(); 38 System.out.println("服务名:" + serviceName); 39 40 NodeList serviceNodes = doc.getElementsByTagNameNS(wsdlNsUri, "service"); 41 Node serviceNode = serviceNodes.item(0); 42 NodeList portNodes = serviceNode.getChildNodes(); 43 44 String bindingAddress = null; 45 Node addressNode = portNodes.item(1).getChildNodes().item(1); 46 bindingAddress = addressNode.getAttributes().getNamedItem("location").getNodeValue(); 47 System.out.println("调用地址:" + bindingAddress); 48 49 NodeList portTypeNodes = doc.getElementsByTagNameNS(wsdlNsUri, "portType"); 50 Node portType = portTypeNodes.item(0); 51 NodeList opNodes = portType.getChildNodes(); 52 for (int i = 0; i < opNodes.getLength(); i++) { 53 Node node = opNodes.item(i); 54 if (node.getNodeType() == Node.ELEMENT_NODE && node.getLocalName().equalsIgnoreCase("operation")) { 55 String methodName = node.getAttributes().getNamedItem("name").getNodeValue(); 56 System.out.println("方法名:" + methodName); 57 } 58 } 59 } 60 61 }
标签:Node,java,doc,NodeList,item,org,import,wsdl,解析 From: https://www.cnblogs.com/lwl80/p/16926772.html