码迷,mamicode.com
首页 > 编程语言 > 详细

java 简单解析wsdl

时间:2015-12-17 17:39:40      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/byxxw/p/5054734.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!