使用Java

时间:2018-08-11 05:55:12

标签: java xsd wsdl wsdl4j

目标:我想阅读WSDL并打印WSDL中的服务,复杂类型和复杂类型定义。

工作:我已经使用WSDL4J来读取WSDL,并且能够成功打印服务及其参数(复杂类型)。现在,我想阅读XSD中可用的复杂类型定义。我无法阅读XSD。还有什么方法吗?

我将XSModel设置为空

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.wsdl.BindingOperation;
import javax.wsdl.Definition;
import javax.wsdl.WSDLException;
import javax.wsdl.xml.WSDLReader;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;

import com.ibm.wsdl.BindingImpl;
import com.ibm.wsdl.xml.WSDLReaderImpl;
import com.sun.org.apache.xerces.internal.impl.xs.XSImplementationImpl;
import com.sun.org.apache.xerces.internal.xs.XSLoader;
import com.sun.org.apache.xerces.internal.xs.XSModel;

public class WSDLDetails {

    public static void main(String[] args) {
        try {
            String wsdlURL = "https://abc.xyz.com/webservice/MessagingSevice?WSDL";
            String xsdURL = "https://abc.xyz.com/webservice/MessagingSevice?xsd=1";
            java.lang.System.setProperty("https.protocols", "TLSv1.2");
            getAllBindingOperation(wsdlURL);
            readXSD(xsdURL);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static List<String> getAllBindingOperation(String wsdlUrl) {
        List<BindingOperation> operationList = new ArrayList();
        List<String> nameList = new ArrayList();
        try {
            WSDLReader reader = new WSDLReaderImpl();
            reader.setFeature("javax.wsdl.verbose", false);
            Definition definition = reader.readWSDL(wsdlUrl.toString());
            Map<String, BindingImpl> defMap = definition.getAllBindings();
            Collection<BindingImpl> collection = defMap.values();
            for (BindingImpl binding : collection) {
                operationList.addAll(binding.getBindingOperations());
            }
            for (BindingOperation operation:operationList) {
                nameList.add(operation.getName());
                System.out.println("Name     :: " + operation.getName());
                System.out.println("Request  :: " + operation.getBindingInput());
                System.out.println("Response :: " + operation.getBindingOutput());
            }
        } catch (WSDLException e) {
            System.out.println("get wsdl operation fail.");
            e.printStackTrace();
        }
        return nameList;
    }

    public static void readXSD(String xsdURL) {
        try {
            System.setProperty(DOMImplementationRegistry.PROPERTY, "com.sun.org.apache.xerces.internal.dom.DOMXSImplementationSourceImpl");
            DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); 
            com.sun.org.apache.xerces.internal.impl.xs.XSImplementationImpl impl = (XSImplementationImpl) registry.getDOMImplementation("XS-Loader");
            XSLoader schemaLoader = impl.createXSLoader(null);
            XSModel model = schemaLoader.loadURI(xsdURL);
            System.out.println(model);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

2 个答案:

答案 0 :(得分:0)

您可以将xsd2java插件与maven一起使用 https://github.com/qaware/xsd2java-gradle-plugin

答案 1 :(得分:0)

这里是一个示例,显示了如何从XSD URL检索XSModel并打印其中声明的复杂类型。

import org.apache.xerces.impl.xs.XMLSchemaLoader;
import org.apache.xerces.impl.xs.XSComplexTypeDecl;
import org.apache.xerces.impl.xs.XSElementDecl;
import org.apache.xerces.xs.XSConstants;
import org.apache.xerces.xs.XSModel;
import org.apache.xerces.xs.XSNamedMap;
import org.apache.xerces.xs.XSTypeDefinition;

public class Test {

  public static void main(String[] args) {
      try {
          String xsdURL = "http://fsharp.github.io/FSharp.Data/data/po.xsd";
          XMLSchemaLoader xsLoader = new XMLSchemaLoader();
          XSModel xsModel = xsLoader.loadURI(xsdURL);

          // print global element declarations
          System.out.println("\nGlobal Element Declarations:");
          XSNamedMap globalElemDecls = xsModel.getComponents(XSConstants.ELEMENT_DECLARATION);
          globalElemDecls.forEach((k,v) -> System.out.println((XSElementDecl) v));

          // print global complex type declarations
          System.out.println("\nGlobal Complex Type Declarations:");
          XSNamedMap globalComplexTypeDecls = xsModel.getComponents(XSTypeDefinition.COMPLEX_TYPE);
          globalComplexTypeDecls.forEach((k,v) -> System.out.println((XSComplexTypeDecl) v));

      } catch (Exception e) {
          e.printStackTrace();
      }
    }
}

如果xsLoader.loadURI(xsdURL)的值为null,则给定XSD文件中可能存在一些缺陷。例如,“ publicId和systemId之间需要空格”。您可能需要首先修复这些缺陷。