解析WSDL的简单方法

我试图解析WSDL以获取操作,端点和示例有效负载。 用户输入的WSDL。 我找不到这样做的教程。

我只能找到生成我不需要的源代码的那些。 我尝试过使用XBeans,但显然我需要Saxon。 没有Saxon,有一种简单的轻量级方法吗?

例如

                                             My first service      

应该获得操作:GetLastTradePrice,GetLastTradePrice

端点:StockQuotePort

样本有效负载:

       

这就像SoapUI所做的那样。 但我主要关心的是能够解析WSDL。 更多上下文是上传WSDL,然后结果显示在GWT应用程序中(文件上载必须转到servlet)。 所以我需要解析文件并创建一个GWT能够理解的对象。

这很好看: http : //www.membrane-soa.org/soa-model-doc/1.4/java-api/parse-wsdl-java-api.htm

虽然我没有为第一次尝试工作,所以我写了一个方法,返回示例wsdl的建议结果 – 没有J2SE6之外的依赖。

 public String[] listOperations(String filename) throws FileNotFoundException, SAXException, IOException, ParserConfigurationException { Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new FileInputStream(filename)); NodeList elements = d.getElementsByTagName("operation"); ArrayList operations = new ArrayList(); for (int i = 0; i < elements.getLength(); i++) { operations.add(elements.item(i).getAttributes().getNamedItem("name").getNodeValue()); } return operations.toArray(new String[operations.size()]); } 

看起来你想要删除重复项,因为每个操作在WSDL中列出两次。 使用Set很容易。 上传完整的eclipse项目,在这里显示唯一和非唯一的结果: https : //github.com/sek/wsdlparser

你好@Rebzie你可以使用JDOM非常容易和轻量级。 我使用解析XML文件。 我希望能帮助你。 🙂

  private static String getMethodNameFromWSDL(String wsdlPath) throws FileNotFoundException, SAXException, IOException, ParserConfigurationException { String tagPrefix = null; String namespace =null; String location = null; NodeList nd = null; Set operations = new HashSet(); NodeList nodeListOfOperations = null; String attr ="http://www.w3.org/2001/XMLSchema" Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(convertURLToString(wsdlPath).getBytes())); NodeList allNodesOfDocumnet = document.getChildNodes(); for(int index = 0;index0)||(document.getElementsByTagName(str2).getLength()>0)){ if(document.getElementsByTagName(tagPrefix+":import").getLength()>0) nd =document.getElementsByTagName(tagPrefix+":import"); else if (document.getElementsByTagName("wsdl:import").getLength()>0) nd =document.getElementsByTagName("wsdl:import"); for (int k = 0; k < nd.item(0).getAttributes().getLength(); k++) { String strAttributes = nd.item(0).getAttributes().item(k).getNodeName(); if(strAttributes.equalsIgnoreCase("namespace")){ namespace = nd.item(0).getAttributes().item(k).getNodeValue(); System.out.println("Namespace : "+namespace); } else { location = nd.item(0).getAttributes().item(k).getNodeValue(); System.out.println("Location : "+location); } } }else{ namespace = document.getFirstChild().getAttributes().getNamedItem("targetNamespace").getNodeValue(); System.out.println("Namespace : "+namespace); } //Getting Operations if((document.getElementsByTagName(str3).getLength()>0)||(document.getElementsByTagName(str4).getLength()>0)){ if(document.getElementsByTagName(str3).getLength()>0){ nodeListOfOperations =document.getElementsByTagName(str3); } else if (document.getElementsByTagName(str4).getLength()>0) { nodeListOfOperations =document.getElementsByTagName(str4); } for (int i = 0; i < nodeListOfOperations.getLength(); i++) { operations.add(nodeListOfOperations.item(i).getAttributes().getNamedItem("name").getNodeValue()); } } if(location!=null){ if(operations.isEmpty()){ Document documentForOperation = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(convertURLToString(location).getBytes())); NodeList nodesOfNewDoc = documentForOperation.getChildNodes(); for(int index = 0;index