6

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

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

我将 XSModel 设为 null

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();
        }
    }
4

2 回答 2

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 之间需要空格”。您可能需要先修复这些缺陷。

于 2019-02-25T14:30:25.900 回答
0

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

于 2019-02-24T15:48:56.580 回答